JavaScript String trim()
- Forrige side toUpperCase()
- Næste side trimEnd()
- Gå tilbage til niveauet over JavaScript String Referencehåndbog
Definition and usage
trim()
The method removes spaces from both sides of the string.
trim()
The method does not change the original string.
Instance
Example 1
Using trim()
Remove spaces:
let text = " Hello World! "; let result = text.trim();
Example 2
Using regular expressions through replace()
Remove spaces:
let text = " Hello World! "; let result = text.replace(/^\s+|\s+$/gm,'');
Syntax
string.trim()
Parameter
No parameters.
Return value
Type | Description |
---|---|
String | A string with spaces removed from both ends. |
Browser support
trim() is an ECMAScript5 (ES5) feature.
All browsers fully support ES5 (JavaScript 2009):
Chrome | IE | Edge | Firefox | Safari | Opera |
---|---|---|---|---|---|
Chrome | IE | Edge | Firefox | Safari | Opera |
Support | 9-11 | Support | Support | Support | Support |
- Forrige side toUpperCase()
- Næste side trimEnd()
- Gå tilbage til niveauet over JavaScript String Referencehåndbog