javascript-trim

Javascript Trim

JavaScript: String trim() method

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

Description

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.

Syntax

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

string.trim();

Parameters or Arguments

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

Returns

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

Note

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

Example

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

For example:

var totn_string = '   datascienceanalysiss   ';

console.log(totn_string.trim());

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:

‘datascienceanalysiss’

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’.