最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Coercion in JavaScript - Stack Overflow

programmeradmin2浏览0评论

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
 |  Show 3 more comments

1 Answer 1

Reset to default 29

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)

发布评论

评论列表(0)

  1. 暂无评论