transformation

String.prototype.trim

Returns a new string with whitespace removed from both ends. Whitespace includes spaces, tabs, newlines, and Unicode whitespace like non-breaking space.

str.trim()

Parameters

Parameter Purpose
(no arguments) Method takes no parameters
returns New string without leading/trailing whitespace

Examples

console.log('   hello   '.trim());

Prints 'hello'

console.log('\n\t hi \r\n'.trim());

Prints 'hi'

console.log('no-trim-needed'.trim());

Prints 'no-trim-needed'

console.log('a b c'.trim());

Prints 'a b c' (interior whitespace untouched)

Gotcha

Only strips leading/trailing whitespace, never interior. Zero-width characters (U+200B) are NOT trimmed — they are not whitespace.

Related methods

← All JS string methods · JS array methods