How to count words in a string ignore spaces with JavaScript

1 Answer

0 votes
const str = "javascript    php  c   c++";
 
const len = str.split(" ").filter(word => word !== '').length;
  
console.log(len); 
  
  
    
      
      
/*
run:
      
4
      
*/

 



answered Feb 15, 2022 by avibootz
...