|| 是什么意思 在 JavaScript 中?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30108466/
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-08-23 04:40:43  来源:igfitidea点击:

What is the meaning of || in javascript?

javascript

提问by bernie2436

I am looking at these lines of code from here:

我正在从这里查看这些代码行:

    if (callback)
        callback(sig || graph);

I have never see vertical "or" bars in a javascript method call. What do they mean? Do they pass the "true" parameter (i.e. sig or graph)? Do they pass the defined parameter? I have never seen that syntax before.

我从未在 javascript 方法调用中看到垂直的“或”条。他们的意思是什么?他们是否传递了“true”参数(即 sig 或 graph)?他们传递定义的参数吗?我以前从未见过这种语法。

回答by ssube

This is the logical OR operator in JS (and most other languages). It is defined in the spec at 11.11. As noted in the spec, expressions on either side will be evaluated first and the logical OR is left-to-right associative. Note that evaluation of the operands follows standard ToBoolean semantics from section 9.2, so [null, undefined, 0, '']all count as falsy.

这是 JS(和大多数其他语言)中的逻辑 OR 运算符。它在 11.11 的规范中定义。如规范中所述,将首先评估任一侧的表达式,并且逻辑 OR 是从左到右关联的。请注意,操作数的计算遵循第 9.2 节中的标准ToBoolean 语义,因此[null, undefined, 0, '']都算作假。

Unlike most languages, JS returns the left operand if it is truthy or the right operand otherwise. This behavior has been covered beforein a number of SO questions, but is worth noting as most languages simply return true or false. This behavior is often used to provide default valuesto otherwise undefined variables.

与大多数语言不同,JS 返回左操作数,如果它为真,则返回右操作数否则。这种行为之前已经在许多 SO 问题中讨论过,但值得注意的是,大多数语言只是简单地返回 true 或 false。此行为通常用于为其他未定义的变量提供默认值

回答by 0x499602D2

The Logical OR operator (||)is an operator that returns its first or second operand depending on whether the first is truthy. A "truthy" value means anything other than 0, undefined, null, "", or false.

逻辑OR运算符(||是根据是否所述第一是truthy其第一或第二操作数中的返回操作。比其他“truthy”值意味着什么0undefinednull"",或false

This operator uses short-circuiting, meaning if the first expression is truthy, then the second expression is not evaluated and the first operand is returned immediately. This is akin to the Logical AND operator (&&), which does the opposite: if the first operand is falsey, it returns it, otherwise it returns the second expression.

此运算符使用短路,这意味着如果第一个表达式为真,则不计算第二个表达式并立即返回第一个操作数。这类似于逻辑与运算符 ( &&),其作用相反:如果第一个操作数为假,则返回它,否则返回第二个表达式。

回答by howderek

It passes whichever evaluates as true, or sigif both are true.

它通过评估为真或sig两者都为真的任何一个。

回答by Ian Wise

It means 'or' (http://www.w3schools.com/js/js_comparisons.asp) So if(sig OR graph)

这意味着“或”(http://www.w3schools.com/js/js_comparisons.asp)所以如果(sig OR图)

BE CAREFULyou can 'short circuit' your code using this. example :

小心你可以使用它“短路”你的代码。例子 :

If (foo || foo2)

if foo is true, then JavaScript wont even test foo2 at all, it just skips it.

如果 foo 为真,那么 JavaScript 甚至根本不会测试 foo2,它只是跳过它。

回答by nurealam siddiq

The double pipe (||) represents OR in JS. In simple words, either this or that is True. It requires any of the sides true to get a Trueresult.

双管(||)在JS中代表OR。简单来说,要么是这样要么是那样True。它需要任何一方为真才能得到True结果。

For example:

例如:

var x = 8;

无功x = 8;

var y = 'c';

var y = 'c';

x >= 8 || y === 'a'

x >= 8 || y === 'a'

The left side of the double pipe returns Truewhere the right side is False. Thus, the result is True.

双管的左侧返回True右侧所在的位置False。因此,结果是True

回答by n.maia

The operator || means OR.

运算符 || 是指或。

If either sig or graph are true or not null variables, callback function will receive a true argument.

如果 sig 或 graph 为真或非空变量,回调函数将收到一个真参数。