Type Coercion in Javascript

2019-08-07  本文已影响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

上一篇 下一篇

猜你喜欢

热点阅读