What exactly is Type Coercion in Javascript?

前端 未结 10 1308
后悔当初
后悔当初 2020-11-22 04:36

What exactly is type coercion in Javascript?

For example, on the use of == instead of ===?

10条回答
  •  温柔的废话
    2020-11-22 05:15

    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.

提交回复
热议问题