15

I was wondering a few things about coercion.

When you do:

1 == true // true

Which one is coerced into which one ? is it the left one or the right one ?

When you do

undefined == null // true

How does it work exactly ? In which order does it try to convert it ? By instance:

1)    String(undefined) == String(null) // false
2)    Number(undefined) == Number(null) // false
3)    Boolean(undefined) == Boolean(null) // true

Does it first try to coerce the left side operand ? then the right ? then both ?

EDIT: As explained in the comments: "not a duplicate. While both questions are about type coercion, this one asks which operand get coerced into the other. The other one is about the source of truth when evaluating the coerced types"

Scipion
  • 11,449
  • 19
  • 74
  • 139
  • 4
    https://developer.mozilla.org/en-US/docs/Web/JavaScript/Equality_comparisons_and_sameness#Loose_equality_using – VLAZ Feb 07 '19 at 06:33
  • 5
    @adiga definitely not a duplicate. While both questions are about type coercion, this one asks which operand get coerced into the other. The other one is about the source of truth when evaluating the coerced types – molamk Feb 07 '19 at 06:41
  • 2
    @adiga Its not a dupe. Marked link is checking equality and this post is asking the process of equality. Its like *Why 1 == true is true* vs *How 1 == true is true* – Rajesh Feb 07 '19 at 06:41
  • 2
    @Rajesh It's a *possible* duplicate. They are related. It's useful future users (and OP) who come to this question and might want to read the linked question. – adiga Feb 07 '19 at 06:45
  • Also relevant: https://stackoverflow.com/a/23465314 – adiga Feb 07 '19 at 07:40
  • 2
    @adiga: Duplicates are "duplicate questions", not "related questions with similar answers". So it's definitely not a duplicate. – Eric Duminil Feb 07 '19 at 08:41
  • @EricDuminil My bad. I'll link them in the comment without voting to close next time. – adiga Feb 07 '19 at 08:42
  • @adiga: Nice. Related questions can definitely be useful in comments, yes. – Eric Duminil Feb 07 '19 at 08:43

1 Answers1

30

The process is described at 7.2.12 Abstract Equality Comparison:

The comparison x == y, where x and y are values, produces true or false. Such a comparison is performed as follows:

  1. If Type(x) is the same as Type(y), then return the result of performing Strict Equality Comparison x === y.

  2. If x is null and y is undefined, return true.

  3. If x is undefined and y is null, return true.

  4. If Type(x) is Number and Type(y) is String, return the result of the comparison x == ToNumber(y).

  5. If Type(x) is String and Type(y) is Number, return the result of the comparison ToNumber(x) == y.

  6. If Type(x) is Boolean, return the result of the comparison ToNumber(x) == y.

  7. If Type(y) is Boolean, return the result of the comparison x == ToNumber(y).

  8. If Type(x) is either String, Number, or Symbol and Type(y) is Object, then return the result of the comparison x == ToPrimitive(y).

  9. If Type(x) is Object and Type(y) is either String, Number, or Symbol, then return the result of the comparison ToPrimitive(x) == y.

  10. Return false.

So rather than coercing one side and then the other, or something like that, it's more that the interpreter goes through that list above until it finds a matching condition, and executes the resulting command, which may involve coercing only the left side, or only the right side (and, rarely, both, in case a recursive command is reached, such as with true == '1', which will fulfill condition 8, turn into 1 == '1', fulfilling condition 6 and turning into 1 == 1, fulfilling condition 3 and resolving to true)

Community
  • 1
  • 1
CertainPerformance
  • 356,069
  • 52
  • 309
  • 320