JavaScript Program to Trim a String

To understand this example, you should have the knowledge of the following JavaScript programming topics:


Example 1: Trim a String

// program to trim a string

const string = '      Hello World       ';

const result = string.trim();

console.log(result);

Output

Hello World

In the above example, the trim() method is used to trim a string.

The trim() method removes white space from both sides of the string.


Example 2: Trim a String Using RegEx

// program to trim a string

function trimString(x) {
    let trimValue = x.replace(/^\s+|\s+$/g,'');
    return trimValue;
}

const result = trimString('    Hello world    ');
console.log(result);

Output

Hello World

In the above program, the RegEx is used with the replace() method to trim the string.

/^\s+|\s+$/g checks for whitespace at the beginning and end of the string.


Also Read:

Did you find this article helpful?

Our premium learning platform, created with over a decade of experience and thousands of feedbacks.

Learn and improve your coding skills like never before.

Try Programiz PRO
  • Interactive Courses
  • Certificates
  • AI Help
  • 2000+ Challenges