JavaScript String trim()
Example
Remove whitespace from both sides of a string:
let str = " Hello World! ";
str.trim() // Returns "Hello World!"
Try it Yourself »
More "Try it Yourself" examples below.
Definition and Usage
The trim() method removes whitespace from both sides of a string.
The trim() method does not change the original string.
Browser Support
toUpperCase() is fully supported in all modern browsers:
| Chrome | IE | Edge | Firefox | Safari | Opera |
| Yes | 9.0 | Yes | Yes | Yes | Yes |
Syntax
string.trim()
Parameters
| None. |
Technical Details
| Return Value: | A String, representing the string with removed whitespace from both ends |
|---|---|
| JavaScript Version: | ECMAScript 5 |
More Examples
Example
For browsers that do not support the trim() method, you can remove whitespaces from both sides of a string with a regular expression:
function myTrim(x) {
return x.replace(/^\s+|\s+$/gm,'');
}
function myFunction() {
var str = myTrim(" Hello World! ");
alert(str);
}
Try it Yourself »

