Table of contents
- 1. Summary
- 2. Syntax
- 3. Parameters
- 4. Description
- 5. Examples
- 5.1. Example: Using trim
- 6. See Also
- 7. Compatibility
- 8. Browser compatibility
Introduced in JavaScript 1.8.1
Summary
Removes whitespace from both ends of the string.
Method of String | |
|---|---|
| Implemented in | JavaScript 1.8.1 |
| ECMAScript Edition | ECMAScript 5th Edition |
Syntax
string.trim()
Parameters
None.
Description
The trim method returns the string stripped of whitespace from both ends. trim does not affect the value of the string itself.
Examples
Example: Using trim
The following example displays the lowercase string "foo":
var orig = " foo "; alert(orig.trim());
See Also
Compatibility
Running the following code before any other code will create String.trim if it's not natively available.
if(!String.prototype.trim) {
String.prototype.trim = function () {
return this.replace(/^\s+|\s+$/g,'');
};
}
Browser compatibility
Based on Kangax's compat tables
| Feature | Firefox (Gecko) | Chrome | Internet Explorer | Opera | Safari |
|---|---|---|---|---|---|
| Basic support | (Supported) | (Supported) | 9 | 10.5 | 5 |
| Feature | Firefox Mobile (Gecko) | Android | IE Mobile | Opera Mobile | Safari Mobile |
|---|---|---|---|---|---|
| Basic support | ? | ? | ? | ? | ? |