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

Spring Elasticsearch Operations

Network Error and Timeout on Authorize.net JS

Object oriented programming concepts (OOPs)