gpt4 book ai didi

Javascript 变量中的 ORing 是如何工作的?

转载 作者:行者123 更新时间:2023-12-02 16:24:35 24 4
gpt4 key购买 nike

我想知道javascript变量中ORing的过程。我使用下面的代码只是为了测试目的。我只想知道 var 中这些 ORing 背后的过程。我知道我们在 if 条件下使用时的过程。请哪位大佬详细解释一下。

var a = a || "2010" || "Gunjan" || 20;
console.log(a);
//output 2010

当我控制台 var a 时,它会给出类似 2010 的输出。

  1. 为什么?

  2. 怎么样?

  3. 谁能给我解释一下后台进程吗?

最佳答案

Logical Operators返回操作数之一的值,因此如果操作数返回非 bool 值,则逻辑运算可能返回非 bool 值

Logical operators are typically used with Boolean (logical) values. When they are, they return a Boolean value. However, the && and || operators actually return the value of one of the specified operands, so if these operators are used with non-Boolean values, they may return a non-Boolean value.

同时查看Short-Circuit EvaluationOR operator一旦其中一个操作数为真,将停止执行进一步的条件。

现在看看你的情况var a = a || “2010”|| “冈詹”|| 20;,既然你使用了var a,我假设你在这里声明了变量a,所以当执行RHS时a 的值为 undefined ,该值是假值,因此 OR 运算符执行第二个操作数,它是一个字符串文字 2010 ,它是一个非 bool 值,但为真值,因此作为此操作的结果返回

关于Javascript 变量中的 ORing 是如何工作的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28803771/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com