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