Does the `===` operator in Javascript have separate definitions for primitives vs non-primitives?

86 Views Asked by At

With the === operator in Javascript, if it operators on primitives, it returns false if either the values are different or the types are different. If it's operating on non-primitives, it returns false if the two operands don't point to the same object.

This seems like the === has separate definitions when applied to primitives and non-primitives. Like "if operands are primitives, do this, else do this". Is there a broader definition of === that encompasses its treatment of both primitives and non-primitives? Like "whether primitives or non-primitive, do this"?

2

There are 2 best solutions below

0
On

The only real answer here is the spec, where we see === defined:

EqualityExpression : EqualityExpression === RelationalExpression

 1. Let lref be ? Evaluation of EqualityExpression.
 2. Let lval be ? GetValue(lref).
 3. Let rref be ? Evaluation of RelationalExpression.
 4. Let rval be ? GetValue(rref).
 5. Return IsStrictlyEqual(rval, lval).

So we're comparing "whatever the GetValue spec function says we should be comparing", paired with "and we have to use the strict equality test for that", so we're not so much comparing "two primitive values, or two references". There's a few more steps involved, which have zero relevance to "actually using JS" in most circumstances, so for practical purposes they simply don't matter...but when you have fundamental questions, the spec is the fundaments =)

0
On

Yes, to some extent - there's a bit of a process here.

7.2.15 IsStrictlyEqual ( x, y )

  1. If Type(x) is different from Type(y), return false.
  2. If x is a Number, then Return Number::equal(x, y).
  3. Return SameValueNonNumber(x, y).

and

7.2.12 SameValueNonNumber ( x, y )

  1. Assert: Type(x) is the same as Type(y).
  2. If x is a BigInt, then a. Return BigInt::equal(x, y).
  3. If x is undefined, return true.
  4. If x is null, return true.
  5. If x is a String, then a. If x and y are exactly the same sequence of code units (same length and same code units at corresponding indices), return true; otherwise, return false.
  6. If x is a Boolean, then a. If x and y are both true or both false, return true; otherwise, return false.
  7. If x is a Symbol, then a. If x and y are both the same Symbol value, return true; otherwise, return false.
  8. If x and y are the same Object value, return true. Otherwise, return false.