Easy Tutorial
❮ Met Text Focus Prop Style Justifycontent ❯

JavaScript trim() Method

JavaScript String Object

Example

Remove whitespace from both ends of a string:

Output result:


Definition and Usage

The trim() method removes whitespace from both ends of a string. Whitespace in this context includes spaces, tabs, and newline characters.

The trim() method does not change the original string.

The trim() method does not work on null, undefined, or Number types.


Browser Support

The numbers in the table specify the first browser version that fully supports the method.

Method
trim() 10.0 9.0 3.5 5.0 10.5

Syntax

string.trim()

Parameter Values

Return Value

Type Description
String Returns the string with leading and trailing whitespace removed.

Technical Details

| JavaScript Version: | ECMAScript 5 | | --- | --- |


More Examples

Example

If your browser does not support the trim() method, you can use a regular expression as an alternative:

Output result:


JavaScript String Object

❮ Met Text Focus Prop Style Justifycontent ❯