||是什么意思在JavaScript中? [英] What is the meaning of || in javascript?

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

问题描述

我正在查看这里

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

我从未在javascript方法调用中看到垂直或栏。他们的意思是什么?他们是否通过了真实参数(即sig或图表)?他们是否通过了定义的参数?我以前从未见过这种语法。

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.

推荐答案

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

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返回左操作数,如果它是真实的或正确的操作数。 之前已在许多SO问题中涵盖了此行为,但值得注意的是,因为大多数语言只返回对或错。此行为通常用于为其他未定义的变量提供默认值

Unlike most languages, JS returns the left operand if it is truthy or the right operand otherwise. This behavior has been covered before in 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 values to otherwise undefined variables.

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

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