totn JavaScript

JavaScript: String trimStart() method

This JavaScript tutorial explains how to use the string method called trimStart() with syntax and examples.

Description

In JavaScript, trimStart() is a string method that is used to remove whitespace characters from the start of a string. Whitespace characters include spaces, tabs, etc. Because the trimStart() method is a method of the String object, it must be invoked through a particular instance of the String class.

Syntax

In JavaScript, the syntax for the trimStart() method is:

string.trimStart();

Parameters or Arguments

There are no parameters or arguments for the trimStart() method.

Returns

The trimStart() method returns a string with whitespace characters removed from the start of the string.

Note

  • The trimStart() method does not change the value of the original string.
  • The trimStart() method can be aliased as the trimLeft() method which also removes whitespace characters from the left side or start of the string.

Example

Let's take a look at an example of how to use the trimStart() method in JavaScript.

For example:

var totn_string = '   TechOnTheNet   ';

console.log(totn_string.trimStart());

In this example, we have declared a variable called totn_string that is assigned the string value of 'TechOnTheNet'. We have then invoked the trimStart() method of the totn_string variable to remove the whitespace characters from the start of the string.

We have written the output of the trimStart() method to the web browser console log, for demonstration purposes, to show what the trimStart() method returns.

The following will be output to the web browser console log:

'TechOnTheNet   '

In this example, the trimStart() method removed the whitespace characters from the start of the string and returned a string value of 'TechOnTheNet   '.