JavaScript Code


How to trim a string in JavaScript?


Trim a string

Given a string, trim the leading and trialing spaces of the string, using JavaScript.

Solution

To trim spaces around a string using JavaScript, call trim() method on the string. The method returns a new string with the leading and trailing spaces removed from the given original string.

Program

1. Given a string in str. Convert the string str to uppercase.

let str = '\n\tHello World    \t \n';
let output = str.trim();
console.log(output);