Cover Image for JavaScript String trim()
127 views

JavaScript String trim()

The trim() method in JavaScript is used to remove whitespace characters from both the beginning and the end of a string. It returns a new string with the leading and trailing whitespace removed.

Syntax:

JavaScript
string.trim()

Example:

JavaScript
var str = "  Hello, World!  ";
var trimmedStr = str.trim();

console.log(trimmedStr); // Output: "Hello, World!"

In this example, the trim() method is called on the str string, which contains leading and trailing whitespace. The resulting trimmedStr variable contains the string with the whitespace removed.

The trim() method is useful when working with user input or manipulating strings that may have unnecessary whitespace. It allows you to clean up the string and remove any unwanted spaces before further processing or displaying the content.

YOU MAY ALSO LIKE...

The Tech Thunder

The Tech Thunder

The Tech Thunder


COMMENTS