Let's say I have an array of words, and I would like to use an object to get the count of it. I tried this:
const results = {};
const words = ["word", "hello", "code", "five", "hello", "word", "new", "code"];
words.forEach( word => {
results[word] = results[word] + 1 || 1;
});
results
in this case returns:
{ word: 2, hello: 2, code: 2, five: 1, new: 1 }
NaN === false
is false
and results[word] + 1 => NaN
. I don't quite understand why results wouldn't be:
{ word: NaN, hello: NaN, code: NaN, five: NaN, new: NaN }
Someone care to explain? :)