Looping through array logic

How does the below logic at line if(!chars[word]) work?

Suppose we have an string that we need to find count of words.

const str = "hello                               world Welcome to hello world"
const chars = {}
const arr = str.split('')
for (let word of arr) {
  if (!chars[word]) {
    chars[word] = 1
  } else {
    chars[word]++
  }
}

console.log(chars)

I want to know how it exactly works like how it finds the number of count.



Comments

Popular posts from this blog

I get wrong characters when retreiving the message body of an email using TIdIMAP4.UIDRetrieveTextPeek2()

How to drop the all the 1's in a correlation matrix

Today Walkin 14th-Sept