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"
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"
Share Improve this question edited Feb 7, 2019 at 9:18 Scipion asked Feb 7, 2019 at 6:30 ScipionScipion 11.9k23 gold badges81 silver badges154 bronze badges 8- 4 developer.mozilla.org/en-US/docs/Web/JavaScript/… – VLAZ Commented Feb 7, 2019 at 6: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 Commented Feb 7, 2019 at 6: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 Commented Feb 7, 2019 at 6: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 Commented Feb 7, 2019 at 6:45
- 2 @adiga: Duplicates are "duplicate questions", not "related questions with similar answers". So it's definitely not a duplicate. – Eric Duminil Commented Feb 7, 2019 at 8:41
1 Answer
Reset to default 29The 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:
If Type(x) is the same as Type(y), then return the result of performing Strict Equality Comparison x === y.
If x is null and y is undefined, return true.
If x is undefined and y is null, return true.
If Type(x) is Number and Type(y) is String, return the result of the comparison x == ToNumber(y).
If Type(x) is String and Type(y) is Number, return the result of the comparison ToNumber(x) == y.
If Type(x) is Boolean, return the result of the comparison ToNumber(x) == y.
If Type(y) is Boolean, return the result of the comparison x == ToNumber(y).
If Type(x) is either String, Number, or Symbol and Type(y) is Object, then return the result of the comparison x == ToPrimitive(y).
If Type(x) is Object and Type(y) is either String, Number, or Symbol, then return the result of the comparison ToPrimitive(x) == y.
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
)