Home >>JavaScript String Methods >JavaScript String trim() Method

JavaScript String trim() Method

JavaScript String trim() Method

JavaScript trim() method is used to remove the whitespace from both the sides of a given input string. It does not take any input arguments. It returns a new string without any leading or trailing white spaces.

Syntax:
string.trim()

Browser Support

Method Chrome Edge Firefox Safari Opera
trim() 10.0 9.0 3.5 5.0 10.5
Here is an example of trim() method:
<html>
<body>
<p>Click the button to see the Output.</p>
<button onclick="myFunction()">Try it</button>
<script>
function myFunction() {
  var str = "     PHPTPOINT     ";
  alert(str.trim());
}
</script>
</body>
</html>
Output:

Click the button to see the Output.

Example 2:
<html>
<body>
<p>Click the button to see the Output.</p>
<button onclick="myFunction1()">Try it</button>
<script>
function myFunction1() {
  var str = "     Abhimanyu     ";
  alert(str.trim());
}
</script>
</body>
</html>
<
Output:

Click the button to see the Output.


No Sidebar ads