Horje
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



Reffered: https://www.geeksforgeeks.org


Geeks Premier League

Related
International Monetary Fund (IMF) International Monetary Fund (IMF)
Convert Unicode String to a Byte String in Python Convert Unicode String to a Byte String in Python
Square Root of 9 | How to Find Value of Square Root of 9? Square Root of 9 | How to Find Value of Square Root of 9?
Filter List of Dictionaries Based on Key Values in Python Filter List of Dictionaries Based on Key Values in Python
React Bootstrap Overlay Component React Bootstrap Overlay Component

Type:
Geek
Category:
Coding
Sub Category:
Tutorial
Uploaded by:
Admin
Views:
13