--- title: String.prototype.trim() slug: Web/JavaScript/Reference/Global_Objects/String/Trim tags: - ECMAScript5 - Prototipo - Referencia - Remover espaços - String - Texto - metodo - trim() translation_of: Web/JavaScript/Reference/Global_Objects/String/Trim --- <div>{{JSRef}}</div> <div>O método <strong><code>trim()</code></strong> remove os espaços em branco (<a href="/pt-BR/docs/DOM/Referencia_do_DOM/Whitespace_in_the_DOM">whitespaces</a>) do início e/ou fim de um texto. É considerado espaço em branco (espaço, tabulação, espaço fixo/rígido, etc.) e todo sinal de fim de linha de texto (LF, CR, etc.).</div> <h2 id="Sintaxe">Sintaxe</h2> <pre class="syntaxbox notranslate"><code><var>str</var>.trim()</code></pre> <h2 id="Descrição">Descrição</h2> <p>O método <code>trim()</code> retorna o texto sem espaços em branco no início e/ou fim da string. O <code>trim()</code> não altera o valor da string original.</p> <h2 id="Exemplos">Exemplos</h2> <h3 id="Usando_trim">Usando <code>trim()</code></h3> <p>Os exemplos a seguir retornam o valor de <code>'foo'</code> sem espaços em branco:</p> <pre class="brush: js notranslate">//.trim() removendo whitespace de ambos os lados var str = ' foo '; console.log(str.trim()); // retorna 'foo' // Outro exemplo de .trim() removendo whitespace de // apenas um lado. var str= 'foo '; console.log(str.trim()); // retorna 'foo' </pre> <h2 id="Polyfill">Polyfill</h2> <p>Executar o seguinte código antes antes de qualquer código irá criar o método <code>trim()</code> se o mesmo não estiver disponível nativamente.</p> <pre class="brush: js notranslate">if (!String.prototype.trim) { String.prototype.trim = function () { return this.replace(/^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g, ''); }; } </pre> <h2 id="Especificações">Especificações</h2> <table class="standard-table"> <tbody> <tr> <th scope="col">Specification</th> <th scope="col">Status</th> <th scope="col">Comment</th> </tr> <tr> <td>{{SpecName('ES5.1', '#sec-15.5.4.20', 'String.prototype.trim')}}</td> <td>{{Spec2('ES5.1')}}</td> <td>Definição inicial. Implementado no JavaScript 1.8.1.</td> </tr> <tr> <td>{{SpecName('ES6', '#sec-string.prototype.trim', 'String.prototype.trim')}}</td> <td>{{Spec2('ES6')}}</td> <td></td> </tr> </tbody> </table> <h2 id="Navegadores_compatíveis">Navegadores compatíveis</h2> <div>{{CompatibilityTable}}</div> <div id="compat-desktop"> <table class="compat-table"> <tbody> <tr> <th>Feature</th> <th>Chrome</th> <th>Firefox (Gecko)</th> <th>Internet Explorer</th> <th>Opera</th> <th>Safari</th> </tr> <tr> <td>Suporte básico</td> <td>{{CompatVersionUnknown}}</td> <td>{{CompatGeckoDesktop("1.9.1")}}</td> <td>{{CompatIE("9")}}</td> <td>{{CompatOpera("10.5")}}</td> <td>{{CompatSafari("5")}}</td> </tr> </tbody> </table> </div> <div id="compat-mobile"> <table class="compat-table"> <tbody> <tr> <th>Feature</th> <th>Android</th> <th>Chrome for Android</th> <th>Firefox Mobile (Gecko)</th> <th>IE Mobile</th> <th>Opera Mobile</th> <th>Safari Mobile</th> </tr> <tr> <td>Suporte básico</td> <td>{{CompatVersionUnknown}}</td> <td>{{CompatVersionUnknown}}</td> <td>{{CompatVersionUnknown}}</td> <td>{{CompatVersionUnknown}}</td> <td>{{CompatVersionUnknown}}</td> <td>{{CompatVersionUnknown}}</td> </tr> </tbody> </table> </div> <h2 id="Veja_também">Veja também</h2> <ul> <li>{{jsxref("String.prototype.trimLeft()")}} {{non-standard_inline}}</li> <li>{{jsxref("String.prototype.trimRight()")}} {{non-standard_inline}}</li> </ul>