JavaScript String trim() method : The trim() method is used to trim the unwanted extra space from both sides (left and right). It returns the new string without having the white-space on both sides. It doesn't change the original string.
Syntax of trim () method :
string.trim()
Example of trim() method : Here is the sample code of how to use trim() method in JavaScript.
<!DOCTYPE html>
<html>
<body>
<p>To remove the whitespace from the string, click the button.</p>
<button onclick="trimSpace()">trim</button>
<script>
function trimSpace() {
var msg = " Hey! Are you ready? ";
alert(msg.trim());
}
</script>
</body>
</html>
Output : Hey! Are you ready?
The output will be in the form of pop-up alert showing the message without the extra spaces.
0 Comment(s)