Friday, October 17, 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
32361 POSTS0 COMMENTS
Milvus
88 POSTS0 COMMENTS
Nango Kala
6728 POSTS0 COMMENTS
Nicole Veronica
11892 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11954 POSTS0 COMMENTS
Shaida Kate Naidoo
6852 POSTS0 COMMENTS
Ted Musemwa
7113 POSTS0 COMMENTS
Thapelo Manthata
6805 POSTS0 COMMENTS
Umr Jansen
6801 POSTS0 COMMENTS