The trim() method in JavaScript is used to remove whitespace characters from both ends of a string. Here's how it works:
Syntax: string.trim()
- string: The string we want to remove whitespace characters from.
Example:
let str = " Hello, World! ";
let trimmedStr = str.trim();
console.log(trimmedStr); // Output: "Hello, World!"
Explanation:
- We have a string str containing leading and trailing whitespace characters.
- We use trim() to remove these leading and trailing whitespace characters.
- The resulting string, "Hello, World!", is stored in the variable trimmedStr and logged to the console.
Code Compiler Link: https://app.singhteekam.in/codecompiler/