In this section, we will learn what the String trim() method is and how to use it in JavaScript.
JavaScript String trim() Method
The `trim()` method is used to remove any white space from both sides of a string value.
Note: the method does not change the original string value but creates a new one with all white spaces removed from both sides of the target string value.
JavaScript String trim() Syntax
string.trim()
String trim() Method Parameters:
The trim() method does not take any argument.
String trim() Method Return Value:
The returned value of this method is a copy of the target string value with white spaces removed from both sides.
Example: remove space from a string
const message = " Hello World! "; const res = message.trim(); console.log(res);
Output:
Hello World!