Javascript Trim

JavaScript: String trim() method

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


In JavaScript, trim() is a string strategy that is utilized to eliminate whitespace characters from the beginning and end of a string. Whitespace characters incorporate spaces, tabs, and so on Since the trim() strategy is a technique for the String object, it should be summoned through a specific example of the String class.


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


Parameters or Arguments

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


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


  • The trim() method does not change the value of the original string.


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

For example:

var totn_string = '   datascienceanalysiss   ';


In this model, we have proclaimed a variable called totn_string that is allocated the string estimation of ‘datascienceanalysiss’. We have then conjured the trim() technique for the totn_string variable to eliminate the whitespace characters from the beginning and end of the string.

We have composed the yield of the trim() technique to the web program reassure log, for exhibit purposes, to show what the trim() strategy returns.

The accompanying will be yield to the web program support log:


In this model, the trim() technique eliminated the whitespace characters from the beginning and end of the string ‘  datascienceanalysiss  ‘ and restored a string estimation of ‘datascienceanalysiss’.