美文网首页
Type Coercion in Javascript

Type Coercion in Javascript

作者: 花括弧 | 来源:发表于2019-08-07 12:37 被阅读0次

Question:
What exactly is type coercion in Javascript?
For example, on the use of == instead of ===?

Answer:
(true == 1) => true / (true === 1) => false

Type coercion means that when the operands of an operator are different types, one of them will be converted to an "equivalent" value of the other operand's type. For instance, if you do:

boolean == integer

the boolean operand will be converted to an integer: false becomes 0, true becomes 1. Then the two values are compared.

However, if you use the non-converting comparison operator ===, no such conversion occurs. When the operands are of different types, this operator returns false, and only compares the values when they're of the same type.

更多详情,请参阅https://github.com/getify/You-Dont-Know-JS/blob/master/types%20%26%20grammar/ch4.md

本文摘自Stack Overflow

相关文章

网友评论

      本文标题:Type Coercion in Javascript

      本文链接:https://www.haomeiwen.com/subject/kdlodctx.html