String.prototype.trim()

Returns a new string with whitespace removed from both ends.

Since ES5 Spec ↗

Syntax

str.trim()

Returns

string — A new string with leading and trailing whitespace removed.

Examples

console.log('  hello  '.trim());
Output
hello
console.log('\t\n x \n'.trim());
Output
x

Notes

Removes all whitespace characters including spaces, tabs, newlines, and no-break spaces. Does not affect internal whitespace. Does not mutate the original string.

Browser & runtime support

EnvironmentSince version
chrome 4
firefox 3.5
safari 5
edge 12
node 0.10

See also