trim()

ES5+

Removes whitespace from both ends of a string.

Syntax

string.trim()

Return Value

string

A new string with whitespace removed from both ends

Examples

JavaScript
const str = '   Hello World   ';
console.log(str.trim());
console.log(str.trim().length);
Output:
// 'Hello World' 11

Related Methods