首页
学习
活动
专区
圈层
工具
发布
社区首页 >专栏 >Type Coercion in Javascript

Type Coercion in Javascript

作者头像
lesM10
发布于 2019-08-26 17:04:13
发布于 2019-08-26 17:04:13
6640
举报

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

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2019.08.07 ,如有侵权请联系 cloudcommunity@tencent.com 删除
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档