JavaScript String trim() Method
Clash Royale CLAN TAG#URR8PPP
<!--
main_leaderboard, all: [728,90][970,90][320,50][468,60]-->
JavaScript String trim() Method
❮
Reference
❯
<!--
❮ JavaScript String Reference
-->Example
Remove whitespace from both sides of a string:
var str = " Hello World! ";
alert(str.trim());
<!--
The alert box will display:
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.
Note: The trim() method does not change the original string.
Browser Support
The numbers in the table specify the first browser version that fully
supports the method.
Method | |||||
---|---|---|---|---|---|
trim() | 10.0 | 9.0 | 3.5 | 5.0 | 10.5 |
Syntax
string.trim()
Parameters
None. |
<!--
mid_content, all: [300,250][336,280][728,90][970,250][970,90][320,50][468,60]-->
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+
function myFunction()
var str = myTrim(" Hello World! ");
alert(str);
Try it Yourself »
❮
Reference
❯
<!--
❮ JavaScript String Reference
-->