This JavaScript tutorial explains how to use the string method referred to as trim() with syntax and examples.
Description
In JavaScript, trim() is a string technique that is used to eliminate whitespace characters from the begin and quit of a string. Whitespace characters consist of spaces, tabs, etc. Because the trim() method is a technique of the String object, it need to be invoked through a specific occasion of the String class.
Syntax
In JavaScript, the syntax for the trim() technique is:
string.trim();
Parameters or Arguments
There are no parameters or arguments for the trim() method.
Returns
The trim() approach returns a string with whitespace characters removed from the start and stop of the string.
Note
The trim() method does not exchange the cost of the authentic string.
Example
Let’s take a seem at an instance of how to use the trim() technique in JavaScript.
For example:
var totn_string = ' TechOnTheNet ';
console.log(totn_string.trim());
In this example, we have declared a variable known as totn_string that is assigned the string value of ‘TechOnTheNet’. We have then invoked the trim() approach of the totn_string variable to cast off the whitespace characters from the start and stop of the string.
We have written the output of the trim() approach to the net browser console log, for demonstration purposes, to show what the trim() technique returns.
The following will be output to the web browser console log:
'TechOnTheNet'
In this example, the trim() technique removed the whitespace characters from the start and quit of the string ‘ TechOnTheNet ‘ and back a string cost of ‘TechOnTheNet’.
Leave a Review