Languages
[Edit]
EN

JavaScript - String trim() method example

8 points
Created by:
Wiktor-Sribiew
800

In this short article, we are going to discuss how to trim strings in JavaScript.

There are 3 different ways to trim string:

  • both sides - most commonly used
  • left side
  • right side

Quick overlook:

// ONLINE-RUNNER:browser;

var text = '    abc    ';

// Both side trimming:
console.log(     'abc' === text.trim()                    ); // true
console.log(     'abc' === text.replace(/^\s*|\s*$/g, '') ); // true

// Left side trimming:
console.log( 'abc    ' === text.replace(/^\s*/g, '') ); // true

// Right side trimming:
console.log( '    abc' === text.replace(/\s*$/g, '') ); // true

Left and right side trimming with ECMAScript 2020:

// ONLINE-RUNNER:browser;

var text = '    abc    ';

// left side trimming:
console.log( 'abc    ' === text.trimStart() ); // true
console.log( 'abc    ' === text.trimLeft()  ); // true

// right side trimming:
console.log( '    abc' === text.trimEnd()   ); // true
console.log( '    abc' === text.trimRight() ); // true

Ā Note: trimStart, trimLeft,Ā trimEnd, trimRightĀ are in draft of ECMAScript 2020, so it is better to use replace approach until theĀ methods will be commonly supported.

More detailed and groupedĀ substringĀ methods description is placed below.

1. Documentation

Syntax
  • String.prototype.trim()
  • String.prototype.trimStart()Ā orĀ String.prototype.trimLeft()
  • String.prototype.trimEnd()Ā orĀ String.prototype.trimRight()
Parameters-
Result

The trim()Ā method returns new string representing the stringĀ stripped of whitespace from both its beginning and end.

The trimStart()Ā andĀ trimEnd()Ā methods returnĀ a new string with whitespace trimmed from just one side.

Description

The trim() method removes whitespace from both ends of a string and returns a new string, without modifying the original one. To trim just one side of the string use trimStart() or trimEnd().

2. Trim operation with built-in methods example

In this section, two ways how to trim strings in javascript are represented. One of them is based on String trim method. The second one replaces white characters with regular expressions.

// ONLINE-RUNNER:browser;

var text = '    abc    ';

console.log( 'abc' === text.trim()                    ); // true
console.log( 'abc' === text.replace(/^\s*|\s*$/g, '') ); // true

3. Side trimming operation with built-in methods example

JavaScript API provides additionally left and right trimming.

3.1. Left side trimming

// ONLINE-RUNNER:browser;

var text = '    abc    ';

console.log( 'abc    ' === text.replace(/^\s*/g, '') ); // true
console.log( 'abc    ' === text.trimStart()          ); // true
console.log( 'abc    ' === text.trimLeft()           ); // true

Note: trimStart and trimLeft are in draft of ECMAScript 2020, so it is better to use replace approach until the methods will be commonly supported.

3.2. RightĀ side trimming

// ONLINE-RUNNER:browser;

var text = '    abc    ';

console.log( '    abc' === text.replace(/\s*$/g, '') ); // true
console.log( '    abc' === text.trimEnd()            ); // true
console.log( '    abc' === text.trimRight()          ); // true

Note: trimEndĀ and trimRightĀ are in draft of ECMAScript 2020, so it is better to use replace approach until the methods will be commonly supported.

Alternative titles

  1. JavaScript - String.prototype.trim() documentation with examples
  2. js - String trim() method documentation with examples
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.

JavaScript - String (documentation)

Native Advertising
šŸš€
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

ā¤ļøšŸ’» šŸ™‚

Join