How to calculate string length without spaces in JavaScript

1 Answer

0 votes
function lengthWithoutSpaces(str) {
    // Remove all spaces using a regular expression
    const stringWithoutSpaces = str.replace(/\s+/g, '');

    return stringWithoutSpaces.length;
}

const str = "JavaScript is interpreted programming language";

const length = lengthWithoutSpaces(str);

console.log(length); 


 
 
/*
run:

42
 
*/

 



answered Jan 11, 2025 by avibootz

Related questions

1 answer 119 views
1 answer 115 views
1 answer 120 views
1 answer 119 views
1 answer 93 views
1 answer 186 views
1 answer 99 views
...