Skip to content
Related Articles
Get the best out of our app
GFG App
Open App
geeksforgeeks
Browser
Continue

Related Articles

JavaScript String trim() Method

Improve Article
Save Article
Like Article
Improve Article
Save Article
Like Article

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

Syntax:

str.trim()

Parameter: This method does not accept any parameter.

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

Below are examples of the String trim() Method. 

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

JavaScript




function func() {
    let str = "GeeksforGeeks     ";
    let st = str.trim();
    console.log(st);
}
func();


Output:

GeeksforGeeks

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

JavaScript




function func() {
 
    // Original string containing whitespace
    let str = "   GeeksforGeeks";
 
    // Trimmed string
    let st = str.trim();
    console.log(st);
}
func();


Output: 

GeeksforGeeks

We have a complete list of Javascript string methods, to check those please go through this Javascript String Complete reference article.

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
My Personal Notes arrow_drop_up
Last Updated : 22 May, 2023
Like Article
Save Article
Similar Reads
Related Tutorials