JavaScript - trim whitespace from string

0 points
Created by:
Admin Dirask Community

In this article, we would like to show you how to trim whitespace from a string in JavaScript.

Quick solution:

// ONLINE-RUNNER:browser;

let text = "   abc   ";

let result = text.trim();



Practical example

In this example, we use trim() method that removes whitespace from both sides of a string.

// ONLINE-RUNNER:browser;

let text1 = "   abc";
let text2 = "abc   ";
let text3 = "   abc   ";

let result1 = text1.trim();
let result2 = text2.trim();
let result3 = text3.trim();

console.log(result1);  // abc
console.log(result2);  // abc
console.log(result3);  // abc


JavaScript - String (popular problems)

JavaScript - trim whitespace from string
Native Advertising
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂