JavaScript(JS) string method - trim

The trim() method is a string method in JavaScript that is used to remove whitespace from both ends of a string.

Here is the syntax for the trim() method:

refer to‮tfigi:‬idea.com
str.trim()

Here, str is the string you want to remove whitespace from.

The trim() method returns the str with all whitespace removed from the beginning and end of the string.

Here is an example of using the trim() method:

let str = "   hello world   ";
let result = str.trim();
console.log(result); // "hello world"

In the example above, the trim() method removes the whitespace at the beginning and end of the str, resulting in the string "hello world".

Note that the trim() method does not modify the original str. Instead, it returns a new string with all whitespace removed from the beginning and end of the string.