DEV Community

mridul037
mridul037

Posted on

1 3

count number of word in string using javascript.

function countWords(str) {
let c=0;
for (x in str){

if(str[x]==" ")

c++;

}
return c+1;
}

Top comments (1)

Collapse
 
delixx profile image
DeLiXx •

or you do

str.split(" ").length

which is not only easier to read but also faster

nextjs tutorial video

Youtube Tutorial Series 📺

So you built a Next.js app, but you need a clear view of the entire operation flow to be able to identify performance bottlenecks before you launch. But how do you get started? Get the essentials on tracing for Next.js from @nikolovlazar in this video series 👀

Watch the Youtube series

👋 Kindness is contagious

Explore a trove of insights in this engaging article, celebrated within our welcoming DEV Community. Developers from every background are invited to join and enhance our shared wisdom.

A genuine "thank you" can truly uplift someone’s day. Feel free to express your gratitude in the comments below!

On DEV, our collective exchange of knowledge lightens the road ahead and strengthens our community bonds. Found something valuable here? A small thank you to the author can make a big difference.

Okay