Javascript Reference - JavaScript String trim() Method








The trim() method removes all leading and trailing white space:

The trim() method does not change the original string.

Browser Support

trim() Yes 9.0 Yes Yes Yes

Syntax

stringObject.trim();

Parameters

None.

Return Value

A string with no whitespace from both ends.





Example


var stringValue = " hello world "; 
var trimmedStringValue = stringValue.trim(); 
console.log(stringValue); //" hello world " 
console.log(trimmedStringValue); //"hello world" 

The original string remains intact.

The code above generates the following result.