We're looking for a person or people to help audit MDN to find places we could speed up. Is this you or someone you know? Check out the RFP: https://mzl.la/2IHcMiE

String.trim Redirect 1


The trim() method removes whitespace from both ends of the string.




The trim method returns the string stripped of whitespace from both ends. trim does not affect the value of the string itself.


Example: Using trim

The following example displays the lowercase string "foo":

var orig = "   foo  ";

console.log( orig.trim() ); // "foo"


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, '');


Specification Status Comment
ECMAScript Language Specification 5.1th Edition (ECMA-262) Standard Initial definition.
Implemented in JavaScript 1.8.1
ECMAScript Language Specification 6th Edition (ECMA-262) Draft  

Browser compatibility

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari
Basic support (Yes) 3.5 (1.9.1) 9 10.5 5
Feature Android Chrome for Android Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile
Basic support (Yes) (Yes) (Yes) (Yes) (Yes) (Yes)

See also

Document Tags and Contributors

 Contributors to this page: Sheppy
 Last updated by: Sheppy,