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
The standard equality operators (== and !=) compare two operands without regard to their type. The strict equality operators (=== and !==) perform equality comparisons on operands of the same type. Use strict equality operators if the operands must be of a specific type as well as value or if the exact type of the operands is important. Otherwise, use the standard equality operators, which allow you to compare the identity of two operands even if they are not of the same type.
With regard to the code, this part:
(element === 0 ? 1 : 0)
...basically says if the value of element
is exactly equal to 0
, then use 1
, otherwise use 0
.
So to take that entire line:
return total + (element === 0 ? 1 : 0);
...the return value of the function will be total + 1
if element
equals 0
, otherwise the return value will be total + 0
.
You could rewrite the code using an if-else
statement:
if( element === 0 ) {
return total + 1;
} else {
return total + 0;
}