.trim()

Remove existing whitespace from both ends of a string.

Syntax

string.trim();

Example

Removing all existing whitespace from both ends of a string using .trim() method:

console.log(' Hello World '.trim());
// Output: Hello World

Contributors

Interested in helping build Docs? Read the Contribution Guide or share your thoughts in this feedback form.

Learn JavaScript on Codecademy

Contributors