JavaScript trim() Method

JavaScript trim() Method removes white spaces from both sides of the string.

Let's say we have a string with spaces on both sides. We can trim these spaces with trim() method.

var string1 = '   ';
var trimmed = string1.trim();
document.getElementById('demo').innerHTML = 'Output: ' + trimmed;


As you can see that spaces are trimmed from both sides.

Video Tutorial

Watch video tutorial on JavaScript trim() Method.