String.prototype.trim()

  • Revision slug: Web/JavaScript/Reference/Global_Objects/String/Trim
  • Revision title: String.trim
  • Revision id: 498467
  • Created:
  • Creator: mitar
  • Is current revision? No
  • Comment Polyfill should match across multiple lines.

Revision Content

{{js_minversion_header("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() );

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+$/gm, '');
  };
}

Browser compatibility

Based on Kangax's compat tables

{{CompatibilityTable}}
Feature Firefox (Gecko) Chrome Internet Explorer Opera Safari
Basic support 3.5 {{CompatVersionUnknown}} 9 10.5 5
Feature Firefox Mobile (Gecko) Android IE Mobile Opera Mobile Safari Mobile
Basic support {{CompatUnknown}} {{CompatUnknown}} {{CompatUnknown}} {{CompatUnknown}} {{CompatUnknown}}

See also

Revision Source

<div>
 {{js_minversion_header("1.8.1")}}</div>
<h2 id="Summary" name="Summary">Summary</h2>
<p>Removes whitespace from both ends of the string.</p>
<table class="standard-table">
 <thead>
  <tr>
   <th class="header" colspan="2">Method of <a href="/en-US/docs/JavaScript/Reference/Global_Objects/String" title="JavaScript/Reference/Global_Objects/String"><code>String</code></a></th>
  </tr>
 </thead>
 <tbody>
  <tr>
   <td>Implemented in</td>
   <td>JavaScript 1.8.1</td>
  </tr>
  <tr>
   <td>ECMAScript Edition</td>
   <td>ECMAScript 5th Edition</td>
  </tr>
 </tbody>
</table>
<h2 id="Syntax" name="Syntax">Syntax</h2>
<pre class="syntaxbox">
<code><em>string</em>.trim()</code></pre>
<h3 id="Parameters" name="Parameters">Parameters</h3>
<p>None.</p>
<h2 id="Description" name="Description">Description</h2>
<p>The <code>trim</code> method returns the string stripped of whitespace from both ends. <code>trim</code> does not affect the value of the string itself.</p>
<h2 id="Examples" name="Examples">Examples</h2>
<h3 id="Example:_Using_toLowerCase" name="Example:_Using_toLowerCase">Example: Using <code>trim</code></h3>
<p>The following example displays the lowercase string "foo":</p>
<pre class="brush: js">
var orig = "&nbsp;&nbsp;&nbsp;foo&nbsp;&nbsp;";

alert( orig.trim() );</pre>
<h2 id="Compatibility" name="Compatibility">Compatibility</h2>
<p>Running the following code before any other code will create <code>String.trim</code> if it's not natively available.</p>
<pre class="brush: js">
if (!String.prototype.trim) {
  String.prototype.trim = function () {
    return this.replace(/^\s+|\s+$/gm, '');
  };
}</pre>
<h2 id="Browser_compatibility" name="Browser_compatibility">Browser compatibility</h2>
<p>Based on <a href="http://kangax.github.com/es5-compat-table/">Kangax's compat tables</a></p>
<div>
 {{CompatibilityTable}}</div>
<div id="compat-desktop">
 <table class="compat-table">
  <tbody>
   <tr>
    <th>Feature</th>
    <th>Firefox (Gecko)</th>
    <th>Chrome</th>
    <th>Internet Explorer</th>
    <th>Opera</th>
    <th>Safari</th>
   </tr>
   <tr>
    <td>Basic support</td>
    <td>3.5</td>
    <td>{{CompatVersionUnknown}}</td>
    <td>9</td>
    <td>10.5</td>
    <td>5</td>
   </tr>
  </tbody>
 </table>
</div>
<div id="compat-mobile">
 <table class="compat-table">
  <tbody>
   <tr>
    <th>Feature</th>
    <th>Firefox Mobile (Gecko)</th>
    <th>Android</th>
    <th>IE Mobile</th>
    <th>Opera Mobile</th>
    <th>Safari Mobile</th>
   </tr>
   <tr>
    <td>Basic support</td>
    <td>{{CompatUnknown}}</td>
    <td>{{CompatUnknown}}</td>
    <td>{{CompatUnknown}}</td>
    <td>{{CompatUnknown}}</td>
    <td>{{CompatUnknown}}</td>
   </tr>
  </tbody>
 </table>
</div>
<h2 id="See_also" name="See_also">See also</h2>
<ul>
 <li><a href="/en-US/docs/JavaScript/Reference/Global_Objects/String/trimLeft" title="JavaScript/Reference/Global Objects/String/trimLeft"><code>trimLeft()</code></a></li>
 <li><a href="/en-US/docs/JavaScript/Reference/Global_Objects/String/trimRight" title="JavaScript/Reference/Global Objects/String/trimRight"><code>trimRight()</code></a></li>
</ul>
Revert to this revision