JavaScript String trimStart() and String trimEnd() Method

String trimStart()

String trimStart() method is used to remove whitespace from the beginning of a string.

The following characters are the whitespace characters:

  1. A space character
  2. A tab character
  3. A carriage return character
  4. A new line character
  5. A vertical tab character
  6. A form feed character

Syntax

string.trimStart()

Parameters

None

Return value

Returns a new string with removed whitespace from the beginning of the string.

Visual RepresentationVisual Representation of JavaScript String trimStart()

Example: How to Use String trimStart() Method

let text = ' Hello Messi ';
console.log(text);
console.log(text.trimStart());

Output

 Hello Messi
Hello Messi

String trimEnd()

String trimEnd() method is used to remove whitespace from the end of a string and returns a new string, without modifying the original string.

Syntax

string.trimEnd()

Parameters

None.

Return value

It returns a new string with removed whitespace from the end of the string.

Visual RepresentationVisual Representation of JavaScript String trimEnd()

Example: How to Use String trimEnd() Method

let text1 = ' Hello Messi '; 
console.log(text1); 
console.log(text1.trimEnd());

Output

 Hello Messi 
 Hello Messi

Browser compatibility

The browsers supported by both methods are listed below:

  1. Google Chrome 66
  2. Firefox 61
  3. Edge 79
  4. Safari 12
  5. Opera 53

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.