What is trim in JavaScript ?
JavaScript trim is a built-in function that is used to trim a string. A string is nothing but a group of characters. The trim ( ) removes whitespaces from both ends i.e. start and end of the string. We have to create an instance for a string class to invoke trim( ). It does not affect the original string.
Syntax:
str.trim()
Parameter:
This method does not accept any parameter.
Example 1: The trim() removes all whitespaces from the string and returns a new string.
Javascript
//string with whitespaces at beginning of the string let mystring = " digitalvasanth" ; //using trim() let newstring = mystring.trim(); console.log(newstring); |
Output
digitalvasanth
Example 2: This example contains white spaces only at the end of string, and by using trim() we have eliminated those spaces.
Javascript
let mystring= "digitalvasanth " ; let newstring=mystring.trim(); console.log(newstring); |
Output
digitalvasanth
Contact Us