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 106 views
1 answer 102 views
1 answer 110 views
1 answer 103 views
1 answer 85 views
1 answer 169 views
1 answer 87 views
...