为什么if语句中的赋值等于true? [英] Why does an assignment in an if statement equate to true?

查看:332
本文介绍了为什么if语句中的赋值等于true?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我开始说我理解======.第一个用于将右侧的值分配给左侧的变量,第二个用于比较两个值的等效性,第三个不仅用于等效性,而且还用于类型比较(即true === 1将返回false).

Let me start off by saying I understand the difference between =, ==, and ===. The first is used to assign the right-hand value to the left-hand variable, the second is used to compare the equivalency of the two values, and the third is used not just for equivalency but type comparison as well (ie true === 1 would return false).

所以我知道几乎每次您看到if (... = ...)时,作者就有很大的机会使用==.

So I know that almost any time you see if (... = ...), there's a pretty good chance the author meant to use ==.

也就是说,我不完全理解这些脚本的作用:

That said, I don't entirely understand what's happening with these scripts:

var a = 5;

if (a = 6)
	console.log("doop");

if (true == 2)
	console.log('doop');

根据此 JavaScript类型等效表true等效于1,但不是0-1.因此,我认为第二个脚本不会输出任何内容(至少在我的Chrome v58.0.3029.110中不存在).

According to this Javascript type equivalency table, true is equivalent to 1 but not 0 or -1. Therefore it makes sense to me that that second script does not output anything (at least, it isn't in my Chrome v58.0.3029.110).

那么为什么第一个脚本输出到控制台,而第二个却不输出呢?第一个脚本的if语句正在评估什么?

So why does the first script output to the console but the second doesn't? What is being evaluated by the first script's if statement?

我挖掘了我的C#知识以帮助我理解,但是在C#中,您无法编译if (a = 5) Console.WriteLine("doop");,因此我必须通过执行if (Convert.ToBoolean(a = 5))将其显式转换为布尔值,但是这很有意义,因为根据对于MSDN文档,如果提供的值不是0,则Convert.ToBool返回true.因此,这对我没有太大帮助,因为在JS中,仅1true是相等的.

I dug into my C# knowledge to help me understand, but in C# you cannot compile if (a = 5) Console.WriteLine("doop"); so I had to explicitly cast it to a bool by doing if (Convert.ToBoolean(a = 5)) but then that makes sense it would evaluate to true because according to MSDN's documentation, Convert.ToBool returns true if the value supplied is anything other than 0. So this didn't help me very much, because in JS only 1 and true are equal.

推荐答案

==进行抽象相等比较与执行从数字值到布尔值的简单类型转换之间是有区别的.在布尔值和数字之间的==比较中,布尔值在比较之前被转换为0或1 .因此在

There's a difference between making an abstract equality comparison with == and performing a simple type cast to boolean from a number value. In a == comparison between a boolean and a number, the boolean value is converted to 0 or 1 before the comparison. Thus in

if (true == 2)

首先将值true转换为1,然后将其与2进行比较.

the value true is first converted to 1 and then compared to 2.

在类似类型转换的情况下

In a type cast situation like

if (x = 2)

该数字将转换为布尔值,以便任何非零值都为true.即,将值2分配给x,并且整个表达式的值是2.然后将其作为布尔值作为if语句的评估的一部分进行测试,然后将其转换为true,因为2不是0.

the number is converted to boolean such that any non-zero value is true. That is, the value 2 is assigned to x and the value of the overall expression is 2. That is then tested as boolean as part of the evaluation of the if statement, and so is converted as true, since 2 is not 0.

评估为布尔值false的各种值是0NaN""nullundefined,当然还有false.当测试为布尔值时(例如在if表达式中),任何值为true.

The various values that evaluate to boolean false are 0, NaN, "", null, undefined, and of course false. Any other value is true when tested as a boolean (for example in an if expression).

这篇关于为什么if语句中的赋值等于true?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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