DEV Community

Discussion on: Comparison & Equolity Operators

Collapse
 
pentacular profile image
pentacular

Equality ( == ) vs. Identity ( === )

Just pointing out that === is not the identity operator.

The === uses the Strict Equality Comparison algorithm, as opposed to == which uses the Abstract Equality Comparison algorithm.

You can easily tell that === is not an identity operator by considering a case like this:

console.log(NaN === NaN);
// False

I'm not sure where the idea that === is an identity operator comes from, but I've seen this mistake repeated many times. :)

Collapse
 
macsikora profile image
Pragmatic Maciej • Edited

Both are equality operators. One exists because of the failure of the second. I would say == is equality with type coercion === is equality without type coercion.

Collapse
 
irenejpopova profile image
Irena Popova πŸ‘©πŸ»β€πŸ’»

JavaScript is performing the equality evaluation. The interpreter first converts both operands to the same type. Then executes the identity comparison.

The identity evaluation algorithm ( ===) some rules

  1. If both operands have different types, they are not strictly equal
  2. If both operands are null, they are strictly equal
  3. If both operands are undefined, they are strictly equal
  4. If one or both operands are NaN, they are not strictly equal
  5. If both operands are true or both false, they are strictly equal
  6. If both operands are numbers and have the same value, they are strictly equal
  7. If both operands are strings and have the same value, they are strictly equal
  8. If both operands have reference to the same object or function, they are strictly equal
  9. In all other cases operands are not strictly equal.

NaN in identity (and in equality) operator compared with any other value always evaluates to false. Let's consider some examples. It’s the best way to remember the strict comparison algorithm.

1 === "1" // false,  

Operands are different types (number and string) and based on rule 1 they are not identical.

0 === 0 // true,  rule  6

Operands are the same type (number) and have the same value, so they are strictly equal based according to the rule in line 6.

undefined === undefined // true,  rule 3

Both operands are undefined and applying rule 3 it’s an equality.

undefined === null // false,  rule 1 

Because operands are different types, based on rule 1 they’re not identical.

NaN === NaN // false,  rule 4 on line 4  -
// identity evaluation algorithm

Operands are the same type (numbers), but the rule 4 indicates than nothing is equal with a NaN. The result isfalse.

var firstObject = {},
  secondObject = firstObject;
secondObject['name'] = 'Tom';
secondObject === firstObject // true, based on  rule 8

Both variables firstObject and secondObject are references to the same object and based on rule 8 the identity operator evaluates to true.

[ ] === [ ] //false,

The [ ] literal creates a new array reference.
Both operands being the same type (object), however have reference to different objects. rule 9 says that the identity evaluates to false.

Collapse
 
pentacular profile image
pentacular

NaN in identity (and in equality) operator compared with any other value always evaluates to false

Again, if NaN === NaN is false, === cannot be an identity operator. :)

Both operands being the same type (object), however have reference to different objects

You'll note that your rules have no "reference to" in them.

[] and [] just have different values - the value of an object is not determined by its properties.