Does javascript have a trim function?

Example 1

Remove spaces with trim():

let text = "       Hello World!        ";
let result = text.trim();

Try it Yourself »

Remove spaces with replace() using a regular expression:

let text = "       Hello World!        ";
let result = text.replace(/^\s+|\s+$/gm,'');

Try it Yourself »


Definition and Usage

The trim() method removes whitespace from both sides of a string.

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


Syntax

Parameters

Return Value

Type Description
A string A string with removed whitespace from both ends.



Browser Support

trim() is an ECMAScript5 (ES5) feature.

ES5 (JavaScript 2009) fully supported in all browsers:

Chrome IE Edge Firefox Safari Opera
Yes 9-11 Yes Yes Yes Yes


The trim() method removes whitespace from both ends of a string and returns a new string, without modifying the original string. Whitespace in this context is all the whitespace characters (space, tab, no-break space, etc.) and all the line terminator characters (LF, CR, etc.).

Try it

Syntax

Return value

A new string representing str stripped of whitespace from both its beginning and end.

If neither the beginning or end of str has any whitespace, a new string is still returned (essentially a copy of str), with no exception being thrown.

To return a new string with whitespace trimmed from just one end, use trimStart() or trimEnd().

Examples

Using trim()

The following example displays the lowercase string 'foo':

const orig = '   foo  ';
console.log(orig.trim()); // 'foo'

Specifications

Specification
ECMAScript Language Specification
# sec-string.prototype.trim

Browser compatibility

BCD tables only load in the browser

See also

The trim() is a built-in string function in JavaScript, which is used to trim a string. This function removes the whitespace from both the ends, i.e., start and end of the string. As the trim() is a string method, so it is invoked by an instance of the String class. We have to create an instance of String class with which the trim() method will be used.

Note - The trim() method does not change the original string; it just removes the leading and trailing whitespace character.

Syntax

The syntax of trim() method is as follows:

Here, str is a String class of object that will contain the string to be trimmed.

Parameters

The trim() method does not have any arguments or parameters inside it.

Return Value

The string.trim() function returns the string after removing whitespace character from that string from the beginning and end of the string.

Examples

Below are some examples, which are using trim() function to remove the elements from it. In these examples, you will see how to use this JavaScript function. So, here are a few examples -

Example 1

In this example, we will pass a string containing whitespace at both ends.

Below is the example of the String trim() Method. 

  • Example:

JavaScript

}

func();

  • Output:
GeeksForGeeks

str.trim() method is used to remove the white spaces from both the ends of the given string.

Syntax:  

str.trim()

Return value: 
This method returns a new string, without any of the leading or the trailing white spaces. 

Examples for the above method are provided below:

Example 1: 

var str = "GeeksforGeeks      ";
var st = str.trim();
print(st);

Output:  

GeeksForGeeks

In this example the trim() method removes all the leading and the trailing spaces in the string str.

Example 2:  

var str = "   GeeksforGeeks";
var st = str.trim();
print(st);

Output:  

GeeksForGeeks

In this example the trim() method removes all the leading and the trailing spaces in the string str.

Codes for the above method are provided below:

Program 1:  

JavaScript

Output: 

GeeksForGeeks

Program 2:  

JavaScript

Output: 

GeeksForGeeks

str.trimLeft() 
str.trimLeft() method is used to remove the white spaces from the start of the given string. It does not affect the trailing white spaces. 

Syntax:  

str.trimLeft()

Return value: 
This method returns a new string, without any of the leading white spaces. 

Examples for the above method are provided below:

Example 1:  

var str = "      GeeksforGeeks      ";
var st = str.trimLeft();
print(st);

Output:  

GeeksForGeeks

In this example the trimLeft() method removes all the leading spaces, but the trailing spaces in the string str remain as they are.

Example 2:  

var str = "   GeeksforGeeks";
var st = str.trim();
print(st);

Output:  

GeeksForGeeks

In this example the trimLeft() method removes all the leading spaces from str.

Codes for the above method are provided below:

Program 1:  

JavaScript

Output: 

GeeksForGeeks       

Program 2: 

JavaScript

Output: 

GeeksForGeeks

str.trimRight() 
str.trimRight() method is used to remove the white spaces from the end of the given string. It does not affect the white spaces at the start of the string.

Syntax: 

str.trimRight()

Return value: 
This method returns a new string, without any of the trailing white spaces. 

Examples for the above method are provided below:

Example 1:  

var str = "GeeksforGeeks      ";
var st = str.trimRight();
print(st);

Output:  

GeeksForGeeks

In this example the trimRight() method removes all the trailing spaces from the string str.

Example 2:  

var str = "   GeeksforGeeks";
var st = str.trimRight();
print(st);

Output:  

     GeeksForGeeks

In this example the trimRight() method does not remove the leading spaces from str.

Codes for the above method are provided below:

Program 1:  

JavaScript

Output: 

GeeksForGeeks       

Program 2: 

JavaScript

Output: 

     GeeksForGeeks

Supported Browser:

  • Chrome 4 and above
  • Edge 12 and above
  • Firefox 3.5 and above
  • Internet Explorer 10 and above
  • Opera 10.5 and above
  • Safari 5 and above

How do you trim a character in JavaScript?

To trim leading and trailing whitespace from a string in JavaScript, you should use the String. prototype. trim() method. The trim() method removes leading and trailing whitespace characters, including tabs and newlines.

How do you trim a sentence in JavaScript?

JavaScript String trim() The trim() method removes whitespace from both sides of a string. The trim() method does not change the original string.

How do you trim an array in JavaScript?

To trim all strings in an array: Use the map() method to iterate over the array and call the trim() method on each array element. The map method will return a new array, containing only strings with the whitespace from both ends removed.

What does trim () do in node JS?

The trim() function is a string function of Node. js which is used to remove white spaces from the string. Parameter: This function does not accepts any parameter. Return Value: The function returns the string without white spaces.