Wednesday, January 21, 2026
HomeLanguagesJavascriptJavaScript String trim() Method

JavaScript String trim() Method

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 = "neveropen     ";
    let st = str.trim();
    console.log(st);
}
func();


Output:

neveropen

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 = "   neveropen";
 
    // Trimmed string
    let st = str.trim();
    console.log(st);
}
func();


Output: 

neveropen

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
Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Dominic
32475 POSTS0 COMMENTS
Milvus
119 POSTS0 COMMENTS
Nango Kala
6847 POSTS0 COMMENTS
Nicole Veronica
11977 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12064 POSTS0 COMMENTS
Shaida Kate Naidoo
6986 POSTS0 COMMENTS
Ted Musemwa
7220 POSTS0 COMMENTS
Thapelo Manthata
6933 POSTS0 COMMENTS
Umr Jansen
6912 POSTS0 COMMENTS