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
Syntax
string.trim()
Return value
A new string.
Examples
console.log(JSON.stringify(" hello \n".trim()));
Output
"hello"
const input = " [email protected] ";
console.log(input.trim().toLowerCase());
Output
[email protected]
Version history
| Version | Change |
|---|---|
| JavaScript ES5 | Added |
In other languages
str.strip()· Pythontrim()· PHPmb_trim()· PHP
Related
string.trimStart()· Remove whitespace (spaces, tabs, line breaks) from the start of a string only, leaving the end untouched.string.trimEnd()· Remove whitespace (spaces, tabs, line breaks) from the end of a string only, leaving the start untouched.- JavaScript Strings · tutorial
- JavaScript String Methods · tutorial