logical-or

How is the nullish coalescing operator (??) different from the logical OR operator (||) in ECMAScript?

╄→尐↘猪︶ㄣ 提交于 2021-01-27 18:48:10
问题 ES2020 introduced the nullish coalescing operator ( ?? ) which returns the right operand if the left operand is null or undefined. This functionality is similar to the logical OR operator ( || ) . For example, the below expressions return the same results. const a = undefined const b = "B" const orOperator = a || b const nullishOperator = a ?? b console.log({ orOperator, nullishOperator }) result: { orOperator:"B", nullishOperator:"B" } So how is the nullish operator different and what is its

Why does the or operator in JavaScript behave differently than in C? [closed]

随声附和 提交于 2019-12-08 15:38:25
问题 Closed . This question is opinion-based. It is not currently accepting answers. Want to improve this question? Update the question so it can be answered with facts and citations by editing this post. Closed 3 months ago . Consider the following code. console.log("All" && 1); // 1 console.log("All" || 1); // "All" ? As you can see, the first expression, "All" && 1 , correctly evaluates to 1 . I expected the second expression, "All" || 1 , to evaluate to 1 too. However, it evaluates to "All" .

Logical or operator not behaving as expected [duplicate]

若如初见. 提交于 2019-12-01 13:48:22
This question already has an answer here: How to test multiple variables against a value? 23 answers def Forest(Health,Hunger): print'You wake up in the middle of the forest' Inventory = 'Inventory: ' Squirrel = 'Squirrel' while True: Choice1 = raw_input('You...\n') if Choice1 == 'Life' or 'life': print('Health: '+str(Health)) print('Hunger: '+str(Hunger)) elif Choice1 == 'Look' or 'look': print 'You see many trees, and what looks like an edible dead Squirrel, \na waterfall to the north and a village to the south.' elif Choice1 == 'Pickup' or 'pickup': p1 = raw_input('Pickup what?\n') if p1 ==

Logical or operator not behaving as expected [duplicate]

 ̄綄美尐妖づ 提交于 2019-12-01 12:58:03
问题 This question already has answers here : How to test multiple variables against a value? (24 answers) Closed 5 years ago . def Forest(Health,Hunger): print'You wake up in the middle of the forest' Inventory = 'Inventory: ' Squirrel = 'Squirrel' while True: Choice1 = raw_input('You...\n') if Choice1 == 'Life' or 'life': print('Health: '+str(Health)) print('Hunger: '+str(Hunger)) elif Choice1 == 'Look' or 'look': print 'You see many trees, and what looks like an edible dead Squirrel, \na

Precedence: Logical or vs. Ternary operator

时光总嘲笑我的痴心妄想 提交于 2019-11-28 12:59:27
Consider the following: (EDIT: I've amended the function slightly to remove the use or braces with the ternary operator) function someFunction(start,end,step){ var start = start || 1, end = end || 100, boolEndBigger = (start < end); // define Boolean here step = step || boolEndBigger ? 1:-1; console.log(step); } someFunction() // step isn't defined so expect (1<10) ? 1:-1 to evaluate to 1 someFunction(1,10) // again step isn't defined so expect to log 1 as before The problem: someFunction(1,10,2) //step IS defined, shortcut logical OR || should kick in, //step should return 2 BUT it returns 1

Precedence: Logical or vs. Ternary operator

随声附和 提交于 2019-11-27 07:21:45
问题 Consider the following: (EDIT: I've amended the function slightly to remove the use or braces with the ternary operator) function someFunction(start,end,step){ var start = start || 1, end = end || 100, boolEndBigger = (start < end); // define Boolean here step = step || boolEndBigger ? 1:-1; console.log(step); } someFunction() // step isn't defined so expect (1<10) ? 1:-1 to evaluate to 1 someFunction(1,10) // again step isn't defined so expect to log 1 as before The problem: someFunction(1

JavaScript if “x = (a || b || c)” statement not working

余生长醉 提交于 2019-11-26 21:07:41
I am making a simple trigonometry program in javascript and my if and while statements are not working properly, as they only pass if the first condition is true i.e. if you type in Sine it will work, but not if you type in Cosine or Tangent. <script language="JavaScript"> var opposite = 1 var adjacent = 1 var hypotenuse = 1 var sct = "SohCahToa" while (!(sct == ("Sine" || "Cosine" || "Tangent"))) { sct = prompt("Sine (unknown adjacent) / Cosine (unkown opposite side) / Tangent (unknown hypotenuse)") if (!(sct == ("Sine" || "Cosine" || "Tangent"))) { alert("Spelling error, please try again") }

JavaScript if “x = (a || b || c)” statement not working

做~自己de王妃 提交于 2019-11-26 07:49:33
问题 I am making a simple trigonometry program in javascript and my if and while statements are not working properly, as they only pass if the first condition is true i.e. if you type in Sine it will work, but not if you type in Cosine or Tangent. <script language=\"JavaScript\"> var opposite = 1 var adjacent = 1 var hypotenuse = 1 var sct = \"SohCahToa\" while (!(sct == (\"Sine\" || \"Cosine\" || \"Tangent\"))) { sct = prompt(\"Sine (unknown adjacent) / Cosine (unkown opposite side) / Tangent