String trim()
In JavaScript, String.trim()
method is used to trim or remove the whitespace characters from the edges of the string.
Syntax
str.trim()
where
str
is a string.
trim()
method makes a copy of the given string, removes whitespace characters from the edges of the string, and returns the resulting string. Original string str
is not modified.
Examples
1. In the following program, we remove the whitespaces around edges of the string value in name
, and print the trimmed string.
let name = ' apple ';
let output = name.trim();
console.log(output);
2. In the following program, we remove the new line and tab space characters at the edges of the string value in name
, and print the trimmed string.
let name = '\n apple \n\n \t ';
let output = name.trim();
console.log(output);