前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Type Coercion in Javascript

Type Coercion in Javascript

作者头像
lesM10
发布2019-08-26 17:04:13
5050
发布2019-08-26 17:04:13
举报
文章被收录于专栏:自译文章/自学记录

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 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档