Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript === (triple equals)

Tags:

javascript

Give the following snippet

function countZeroes(array) {
  function counter(total, element) {
    return total + (element === 0 ? 1 : 0);
  }
  return reduce(counter, 0, array);
}
  1. What does the === do?
  2. Is reduce a built in function? What does it do?
  3. Please explain the steps of this program.
like image 425
mjmitche Avatar asked Feb 25 '11 02:02

mjmitche


People also ask

What is the difference between double == and triple === equals?

Turns out, the difference between them is the double equals allows coercion and the triple equals disallows coercion. They both test equality. One of them allows coercion to occur first and one of them doesn't.

Should I use === or ==?

The difference between == and === is that: == converts the variable values to the same type before performing comparison. This is called type coercion. === does not do any type conversion (coercion) and returns true only if both values and types are identical for the two variables being compared.

Why does JavaScript equal 3?

The triple equals sign in JavaScript means “equality without type coersion”. That means, the type and values must both be equal. Take for example the scenario where 0 is false. If we compare the same 0 and false with ===, we have false returned.

Why would you use === instead of ==?

== Operator: This operator is used to check the given values are equal or not. If yes, it returns true, otherwise it returns false. === Operator: This operator is used to check the given values and its data type are equal or not. If yes, then it returns true, otherwise it returns false.


1 Answers

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 Absract 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 MDC 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;
}
like image 104
user113716 Avatar answered Oct 03 '22 06:10

user113716