Javascript假值(null,undefined,false,空字符串:""或''和0)和比较(==)运算符 [英] Javascript falsy values (null, undefined, false, empty string: "" or '' and 0) and comparison(==) operator

查看:70
本文介绍了Javascript假值(null,undefined,false,空字符串:""或''和0)和比较(==)运算符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我使用任何一个值时( null undefined false '' 0 )在中如果语句,它总是被评估为谬误( false )。此外,否定这些值(( null undefined false '' 0 )在中如果语句总是被评估为重言式( true )。

When I am using any one of values(null, undefined, false, '', 0) in a if statement, it is always evaluated as fallacy(false). Also, the negation of these values((null, undefined, false, '', 0) in a if statement always evaluated as tautology(true).

 if(null){
 }else{
 }

 if(undefined){
 }else{
 }

 if(false){
 }else{
 }

 if(''){
 }else{
 }

 if(0){
 }else{
 }

在以上所有情况下, 如果语句被评估为 false & else 语句执行。
但是,当我将这些谬误值与 == 运算符进行比较时,它不会返回 true 总是。令人惊讶的是,当我比较这些值的否定时,它总是返回 true 值。

In all the above cases, if statement is evaluated as false & else statement executes. However, when I am comparing these fallacy values with == operator, it is not returning true always. Surprisingly, it is always returning true values when I am comparing the negation of these values.

如果加倍equalto( == )运算符检查/比较值,而不是严格的类型,那么原因:

if double equalto (==) operator checks/compares for values & not strictly for types, then why:

null == false          // returns false

null == 0              // returns false

null == ''             // returns false

但是,

!null == !false       // returns true

!null == !0           // returns true

!false == !undefined  // returns true

并且,

null == undefined     // returns true

false == 0            // returns true

如果有人能澄清这些值之间的行为或关系,我很感激( null 未定义 false '' 0 )。

I appreciate if any one can clarify the behavior or relationship among these values(null, undefined, false, '', 0).

推荐答案

一个常见的误解



A common misconception


...如果double equalto( == ),运算符仅检查/比较值&不适用于类型...

"...If double equalto (==) operator only checks/compares for values & not for types..."

这是一个不正确的假设,尽管它经常被人重复。实际上, == 检查类型,实际上比 === 比较。

That's an incorrect assumption, though it's often repeated by people. In reality, the == does check types, and in fact pays far more attention to the types than a === comparison does.

参见 抽象平等比较算法

A == 比较不做简单的 toBoolean 转换。而是它遍历一个有点复杂的递归算法,在检查类型后,如果它们不匹配,则尝试将操作数强制转换为相同的类型。

A == comparison doesn't do a simple toBoolean conversion. Rather it walks through a somewhat complex recursive algorithm, which, after checking the types, attempts to coerce the operands to the same type if they don't match.

它执行的类型强制非常特定于操作数的类型。对于不同的类型对,可以发生不同的强制序列。通常(但不总是)它最终会强制将操作数强制转换为数字类型。

The type coercion that it performs is very specific to the types of the operands. A different sequence of coercions can take place for different type pairs. Usually (but not always) it ends up ultimately coercing the operands down to number types.

当你使用手动强制两个操作数时,你现在正在做一个简单的 toBoolean 转换导致类型匹配,这避免了算法的类型强制部分,使其行为基本上类似于 严格的等式比较算法

When you manually coerce both operands using !, you're now doing a simple toBoolean conversion causing the types to match, which avoids the type coercive part of the algorithm, making it behave essentially like the Strict Equality Comparison Algorithm.

所以预测结果的唯一方法== 比较当类型不匹配时,要理解抽象算法。

So the only way to predict the outcome of a == comparison when the types don't match is to understand that Abstract algorithm.

还有,还有一个值得考虑的假值, NaN的。它的 == 比较总是 false ,无论如何。即使与另一个 NaN 值进行比较,它也将是 false

And FYI, there's one more "falsey" value to consider, NaN. Its == comparison will always be false, no matter what. Even when comparing to another NaN value, it'll be false.

这篇关于Javascript假值(null,undefined,false,空字符串:""或''和0)和比较(==)运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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