简体   繁体   English

||的正确用法是什么? 在JavaScript中

[英]What is the correct use of the || in javascript

In the code below, because s is null d = "test" but if s = "hello" then d would = "hello". 在下面的代码中,因为s为空d =“ test”,但是如果s =“ hello”,则d =“ hello”。

Is this correct as it works? 这是正确的吗? what is the correct way to use || ||的正确使用方法是什么?

var s = null;

var d = s || "test";

alert(d);

|| || is "or" ; 是“或”; and understanding what happens here is a bit trickery 理解这里发生的事情有点诡计多端

var a=false;
var b=true;
result=a||b

will give "result" true (as b is true). 将给出“结果”为真(因为b为真)。 What happens is: 发生的是:

  • 'a' is checked - it is false 选中“ a”-错误
  • 'b' is checked AFTERWARDS (as no "true" result has been obtained yet, and ONE "true" result would suffice to make the whole || operator be true) - the value of it will be assigned to the left side 之后再检查“ b”(因为尚未获得“真”结果,而一个“真”结果足以使整个||运算符为真)-将其值赋给左侧

if you had 如果你有

   var a=true;
   var b="test";
   result=a||b

result will yield true; 结果将为真; as no other value needs to be checked by the logic of "||" 因为不需要通过“ ||”逻辑检查其他值

with

var a=null;
var b="test";
result=a||b;

a will be checked first - it is null, which converts to "false". 将首先检查a-它为null,它将转换为“ false”。 b is "test", which is non-null, and converts to "true". b是“ test”,非空,并转换为“ true”。 so the value of b will be assigned. 因此将分配b的

And yes, this is a correct way to use || 是的,这是使用||的正确方法。 ; ; the feature you use is also called short-circuit evaluation (as it stops evaluating the boolean expression as early as possible) 您使用的功能也称为短路评估(因为它会尽早停止评估布尔表达式)

This works, but if s evaluates to a 'falsy' value, you'll get your default, which might not be what you intended. 这可以工作,但是如果s评估为“ fassy”值,则将获得默认值,这可能不是您想要的。 A more robust, but wordy idiom is 一个更健壮但罗word的习语是

d = (typeof s === "undefined") ? "test" : s;

Yes it is correct unless s is allowed to be blank or 0 which are also falsy values 是的,除非s允许为空白或0(也是伪造的值),否则它是正确的

var s = null;
var d = s || "test";

var s = 0;
var d = s || "test";

var s = "";
var d = s || "test";

All will result in d being "test" 所有都会导致d被“测试”

|| is a logical operator. 是逻辑运算符。 When s is not null then the condition of (s) is true so d is assigned the value of s, otherwise it is assigned 'test' 当s不为null时,则(s)的条件为true,因此为d分配s的值,否则将其分配为'test'

|| is the OR operator in javascript 是javascript中的OR运算子
so a||b means a OR b in simple terms 所以a||b表示简单a OR b
explanation of question you have asked is that id you simply do somethings like these in js you will ultimately get in the else block 您所问的问题的解释是id只是在js中做类似的事情,最终您会进入else块

if(null)
if(undefined)

so s||"test" will mean which ever is not null or undefined which in this case is test 所以s||"test"表示不是null或未定义的那个,在这种情况下是test

yes correct, || 是的,|| symbols just does the job of OR. 符号只是完成“或”的工作。 when the first condition is true its gonna return that one.. else it will move to the next... simple as it is... 当第一个条件为真时,它将返回那个条件..否则它将移至下一个条件...就这么简单...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 javascript .onclick 函数的正确方法是什么? - What is the correct way to use the javascript .onclick function? 在这部分javascript中使用onclick的正确方法是什么? - what is the correct way to use onclick in this part of javascript? 在JavaScript中使用数组的正确方法是什么? - What is the correct way to use an Array in Javascript? 使用JavaScript对DOM对象执行算术运算的正确方法是什么? - What is the correct way to use JavaScript to perform arithmetic on a DOM object? 在 JavaScript 中,用于解析特定字符串的正确“时间”类型是什么? - In JavaScript, what is the correct `Temporal` type to use for parsing a particular string? 使用JavaScript查询项目组合项目的正确“类型”是什么 - What is the correct “Type” to use for querying portfolio items using JavaScript 在 JavaScript RegExp 中使用数组的正确方法是什么? - What's the correct way to use an array in a JavaScript RegExp? 在javascript中“序列化”函数以供以后使用的正确方法是什么 - What is the correct way to “serialize” functions in javascript for later use HTML5 在锚点中使用 Javascript 的正确方法是什么? - What is the correct way in HTML5 to use Javascript in anchors? 什么是正确的Javascript语法 - What is the Correct Javascript Syntax for this
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM