// define a string with multiple whitespace
const str = " Programming Is Fun ";
// remove whitespace using regex
const result = str.replace(/s/g, '');
// print the result
console.log(result);
Output
ProgrammingIsFun
Explanation
We can also use the repalceAll() function of Javascript to remove all the white spaces from a string. The repalceAll() function takes two arguments - separator and joiner.
separator: The String that you want to replace.
joiner: The string that will be placed in the place of the separator.
Syntax
String.replaceAll(separator, joiner)
Code Example
const str = " Programming Is Fun ";
const result = str.replaceAll(' ', '');
console.log(result);
Output
ProgrammingIsFun
const str = " Programming Is Fun ";
const result = str.split(' ').join('')
console.log(result);
0 Comments