javascript console.log() 语句中的布尔条件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33253254/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 16:16:32  来源:igfitidea点击:

Boolean conditions in console.log() statement

javascriptconsole.log

提问by breaktop

I've just noticed that you can put boolean expressions within console.log e.g. console.log("hey" || 2)where in this caseheywould be printed out to the console window.

我刚刚注意到您可以在 console.log 中放置布尔表达式,例如console.log("hey" || 2)在这种情况下hey将打印到控制台窗口。

I'm not 100% sure exactly how console.logdetermines what to print when there is a condition within it.

我不是 100% 确定console.log当其中存在条件时如何确定要打印的内容。

回答by Bohuslav Burghardt

There is a concept of truthyand falsyvalues in JavaScript. Non empty string is considered truthyvalue, so "hey"evaluates to trueand is printed, because the part after ||is not evaluated in that case.

有一个概念truthyfalsy在JavaScript中值。非空字符串被认为是值,因此"hey"评估为true并打印,因为||在这种情况下不会评估后面的部分。

In general truthyare all values that are not false, 0, "", null, undefined, or NaN.

一般来说,truthy是所有不是false, 0, "", null, undefined, 或 的值NaN

MDNdefines evaluation of OR expressions as follows:

MDN定义 OR 表达式的计算如下:

Logical OR (||) expr1 || expr2: Returns expr1if it can be converted to true; otherwise, returns expr2. Thus, when used with Boolean values, ||returns true if either operand is true; if both are false, returns false.

逻辑 OR ( ||) expr1 || expr2:返回expr1是否可以转换为真;否则,返回expr2。因此,当与布尔值一起使用时, ||如果任一操作数为真则返回真;如果两者都为假,则返回假。

So by this logic console.log()prints first truthy expression in your statement. If you were to try console.log(null || 2), then 2would be printed out.

因此,通过这种逻辑console.log()在您的语句中打印第一个真实表达式。如果您要尝试console.log(null || 2),则将2打印出来。

回答by Ramasamy Kanna

if the a value is falsy(false, undefined, 0, NaN and "") then it will take the right side value.else it will get print the a itself.

如果 a 值是假的(false, undefined, 0, NaN and "") 那么它会取右边的值。否则它会打印 a 本身。

var a = null;

console.log(a || 10); //10 will print

or

或者

var a = 20;

console.log(a || 10);//20 will print

回答by GaborHorvath

Well, let me try to add my two cents to Bohuslav's great explanation... If you want to see a good practical example, check out this exercise from the book Eloquent Javascript:

好吧,让我尝试将我的两分钱添加到 Bohuslav 的精彩解释中……如果您想看到一个很好的实际示例,请查看Eloquent Javascript一书中的这个练习:

"FizzBuzz

“嘶嘶声

Write a program that uses console.log to print all the numbers from 1 to 100, with two exceptions. For numbers divisible by 3, print "Fizz" instead of the number, and for numbers divisible by 5 (and not 3), print "Buzz" instead.

编写一个程序,使用 console.log 打印从 1 到 100 的所有数字,但有两个例外。对于可以被 3 整除的数字,打印“Fizz”而不是数字,对于可以被 5(而不是 3)整除的数字,打印“Buzz”代替。

When you have that working, modify your program to print "FizzBuzz", for numbers that are divisible by both 3 and 5 (and still print "Fizz" or "Buzz" for numbers divisible by only one of those)."

当你有那个工作时,修改你的程序以打印“FizzBu​​zz”,对于可以被 3 和 5 整除的数字(对于只能被其中一个整除的数字,仍然打印“Fizz”或“Buzz”)。”

And the solution:

解决方案

for (var n = 1; n <= 100; n++) {
    var output = "";
    if (n % 3 == 0)
        output += "Fizz";
    if (n % 5 == 0)
        output += "Buzz";
    console.log(output || n);
}

Here the variable outputis an empty string, so the console log won't print it, as empty strings are falsy; but as soon as "Fizz" or "Buzz" is added to its value, it becomes truthy and gets printed out instead of n.

这里的变量output是一个空字符串,所以控制台日志不会打印它,因为空字符串是假的;但是,一旦将“Fizz”或“Buzz”添加到其值中,它就会变为真值并被打印出来而不是n.

Note that it wouldn't work the other way around with: console.log(n || output). As nis the first expression and always true, it will be printed every time and outputwill be ignored.

请注意,它不适用于:console.log(n || output)。作为n第一个表达式并且始终为真,它每次都会被打印并被output忽略。

回答by Kamen Minkov

Most simply put, when a logical operator exists in the expression, it will try to evaluate the whole expression - and since "||" means "or", when "hey" is evaluated, it returns true, hence there is no need to evaluate the expression further.

最简单地说,当表达式中存在逻辑运算符时,它将尝试评估整个表达式 - 因为“||” 表示“或”,当计算“hey”时,它返回true,因此无需进一步计算表达式。