Home » JavaScript String trim()

JavaScript String trim()

by Online Tutorials Library

JavaScript String trim()

The trim() is a built-in string function in JavaScript, which is used to trim a string. This function removes the whitespace from both the ends, i.e., start and end of the string. As the trim() is a string method, so it is invoked by an instance of the String class. We have to create an instance of String class with which the trim() method will be used.

Note – The trim() method does not change the original string; it just removes the leading and trailing whitespace character.

Syntax

The syntax of trim() method is as follows:

Here, str is a String class of object that will contain the string to be trimmed.

Parameters

The trim() method does not have any arguments or parameters inside it.

Return Value

The string.trim() function returns the string after removing whitespace character from that string from the beginning and end of the string.

Examples

Below are some examples, which are using trim() function to remove the elements from it. In these examples, you will see how to use this JavaScript function. So, here are a few examples –

Example 1

In this example, we will pass a string containing whitespace at both ends.

Test it Now

Output

This will return the below string after removing whitespace.

tutoraspire tutorial website  

Example 2

In this example, we will pass a string containing whitespace only at the end.

Test it Now

Output

This will remove the whitespace from the end and return the string as given below.

tutoraspire tutorial website  

Example 3

In this example, we will pass a string containing whitespace only in the beginning.

Test it Now

Output

This will remove the whitespace from the beginning and return the string as given below.

tutoraspire tutorial website  

JavaScript offers two more functions similar to the trim() method. They also used to trim the string but only from the one end, either left or right. Remember that both the functions do not change the original string; they just remove the whitespaces.

  • trimLeft()
  • trimRight()

trimLeft()

The trimLeft() method removes the whitespace only from the left of the string. To understand better, we can say that it removes whitespace only from the start and returns the string back without any leading whitespace character.

Syntax

See the example below –

Example 1

Test it Now

Output

This will remove the whitespace only from the beginning and trailing whitespace will remain the same. See the output below:

tutoraspire tutorial website  

trimRight()

On the other hand, the trimRight() method removes the whitespace only from the right of the string. To understand better, we can say that it removes whitespace only from the end and returns the string back without any trailing whitespace character.

Syntax

See the example below –

Example 1

Test it Now

Output

This will remove the whitespace only from the end and leading whitespace will remain the same. See the output below:

tutoraspire tutorial website  

Next TopicJavaScript timer

You may also like