javascript counting duplicates in array

Viewed 4217

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.

3 Answers
Related