The trim() method removes whitespace from both sides of a string.
The trim() method removes whitespace from both sides of a string.
The trim() method does not change the original string.
string.trim()
None
A String, representing the string with removed whitespace from both ends
Remove whitespace from both sides of a string:
//alert the string with removed whitespace. var str = " Hello World! "; console.log(">"+str.trim()+"<"); //you can remove whitespaces from both sides of a string with a regular expression: function myTrim(x) { return x.replace(/^\s+|\s+$/gm,''); } var str = myTrim(" Hello World! "); console.log(">"+str+"<");