JavaScript (ES2026) reference · String Methods

JavaScript String trim() Method

Remove whitespace (spaces, tabs, line breaks) from both ends of a string.

JavaScript (ES2026) Since ES5

Create a free account to save this page, take notes and track your progress. Sign in

Syntax

string.trim()

Return value

A new string.

Examples

Trim both endsTry it yourself
console.log(JSON.stringify("  hello \n".trim()));

Output

"hello"
Clean user inputTry it yourself
const input = "  [email protected] ";
console.log(input.trim().toLowerCase());

Version history

VersionChange
JavaScript ES5Added

In other languages

Was this page helpful?