Javascript中的Type Coercion究竟是什么? [英] What exactly is Type Coercion in Javascript?

查看:134
本文介绍了Javascript中的Type Coercion究竟是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Javascript中的类型强制究竟是什么?

What exactly is type coercion in Javascript?

例如,使用 == 而不是 ===

推荐答案

类型强制意味着当操作数为运算符是不同的类型,其中一个将转换为另一个操作数类型的等效值。例如,如果你这样做:

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

布尔操作数将转换为整数: false 变为 0 true 变为1.然后比较两个值。

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

但是,如果你使用非 - 转换比较运算符 === ,不会发生此类转换。当操作数具有不同类型时,此运算符返回 false ,并仅在它们属于同一类型时进行比较。

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.

这篇关于Javascript中的Type Coercion究竟是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆