10 August 2024

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
  1. let str = " Hello, world! ";
  2. let trimmedStr = str.trim();
  3. console.log(trimmedStr); // Output: "Hello, world!"
Programming Language: ECMAScript