Friday, September 5, 2025
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
32269 POSTS0 COMMENTS
Milvus
81 POSTS0 COMMENTS
Nango Kala
6637 POSTS0 COMMENTS
Nicole Veronica
11802 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11865 POSTS0 COMMENTS
Shaida Kate Naidoo
6752 POSTS0 COMMENTS
Ted Musemwa
7027 POSTS0 COMMENTS
Thapelo Manthata
6704 POSTS0 COMMENTS
Umr Jansen
6721 POSTS0 COMMENTS