In JavaScript, you can trim whitespace from the beginning and end of a string using the trim() method. This example shows you how it will remove any leading and trailing whitespace characters (spaces, tabs, etc.) from the string str.
Source code viewer
let str = " Hello, world! "; let trimmedStr = str.trim(); console.log(trimmedStr); // Output: "Hello, world!"Programming Language: ECMAScript