Given the following snippet:
function countZeroes(array) {
function counter(total, element) {
return total + (element === 0 ? 1 : 0);
}
return reduce(counter, 0, array);
}
- What does
===do? - Is
reducea built-in function? What does it do? - Please explain the steps of this program.
It is the strict equality operator.
It compares two values and checks to see if they are identical according to the Strict Equality Comparison Algorithm.
This is opposed to
==, which will attempt to coerce one or both of the values being compared if they are of different types. That one uses the Abstract Equality Comparison Algorithm.The rules for the Abstract algorithm can be tricky. You’re better off using
===unless you have special need for==.From the MDN docs
With regard to the code, this part:
…basically says if the value of
elementis exactly equal to0, then use1, otherwise use0.So to take that entire line:
…the return value of the function will be
total + 1ifelementequals0, otherwise the return value will betotal + 0.You could rewrite the code using an
if-elsestatement: