Please consider this JS function
I have seen this similar question but couldn't understand.
function a (ResultArray){
var counts={};
for ( p = 0; p < ResultArray.length; p++){
counts[ResultArray[p]] = (counts[ResultArray[p]] + 1) || 1;
}
return counts;
}
var arr = ["a","b","c","d","a","b","e"];
var res = new a(arr);
console.log(res)
Its working fine and giving the count. I need to understand how is it giving the count, specially (counts[ResultArray[p]] + 1) || 1; part. what is +1 and || doing.
1. --- Honestly, it's a pretty strange looking line of code.+ 1is adding 1 tocounts[ResultArray[p]]and||is a truthy check: ifcounts[ResultArray[p]] + 1isNaN, undefined, null, or 0the expression will evaluate to1see this post for greater detail