or-operator

Logical operator || in javascript, 0 stands for Boolean false?

眉间皱痕 提交于 2019-11-27 05:12:44
I happened to know the following code Here is the code, and very simple: var test = 0 || -1 ; console.log(test); then the output in the console is -1 and somehow i am really new into the javascript, all i think of is that the 0 stands for Boolean False in JS ,and so || operator seems to ignore the 0 and assign the value -1 to the variable so am i right ? i just want a confirm gdoron || — expr1 || expr2 (Logical OR) Returns expr1 if it can be converted to true; otherwise, returns expr2. Thus, when used with Boolean values, || returns true if either operand is true; if both are false, returns

Switch statement using or

99封情书 提交于 2019-11-27 03:45:55
问题 I'm creating a console app and using a switch statement to create a simple menu system. User input is in the form of a single character that displays on-screen as a capital letter. However, I do want the program to accept both lower- and upper-case characters. I understand that switch statements are used to compare against constants, but is it possible to do something like the following? switch(menuChoice) { case ('q' || 'Q'): //Some code break; case ('s' || 'S'): //More code break; default:

The behaviour of the or operator in PHP

限于喜欢 提交于 2019-11-26 19:48:22
问题 I'm trying to understand the behavior of or operator. Please see the below examples: $e = false || true; var_dump($e); Output is as expected: bool(true); $f = false or true; var_dump($f); Output is as expected: bool(false) . I understood this in a way that the = has a higher precedence than the Or , so that's why the $f is assigned to false . But the below code works quite opposite of what I thought. I thought that the $foo will be assigned to 5 and then compared to itself. But the $foo is

Shortcut “or-assignment” (|=) operator in Java

守給你的承諾、 提交于 2019-11-26 14:23:16
I have a long set of comparisons to do in Java, and I'd like to know if one or more of them come out as true. The string of comparisons was long and difficult to read, so I broke it up for readability, and automatically went to use a shortcut operator |= rather than negativeValue = negativeValue || boolean . boolean negativeValue = false; negativeValue |= (defaultStock < 0); negativeValue |= (defaultWholesale < 0); negativeValue |= (defaultRetail < 0); negativeValue |= (defaultDelivery < 0); I expect negativeValue to be true if any of the default<something> values are negative. Is this valid?

JavaScript OR (||) variable assignment explanation

匆匆过客 提交于 2019-11-26 11:25:34
Given this snippet of JavaScript... var a; var b = null; var c = undefined; var d = 4; var e = 'five'; var f = a || b || c || d || e; alert(f); // 4 Can someone please explain to me what this technique is called (my best guess is in the title of this question!)? And how/why it works exactly? My understanding is that variable f will be assigned the nearest value (from left to right) of the first variable that has a value that isn't either null or undefined, but I've not managed to find much reference material about this technique and have seen it used a lot. Also, is this technique specific to

JavaScript OR (||) variable assignment explanation

試著忘記壹切 提交于 2019-11-26 02:25:17
问题 Given this snippet of JavaScript... var a; var b = null; var c = undefined; var d = 4; var e = \'five\'; var f = a || b || c || d || e; alert(f); // 4 Can someone please explain to me what this technique is called (my best guess is in the title of this question!)? And how/why it works exactly? My understanding is that variable f will be assigned the nearest value (from left to right) of the first variable that has a value that isn\'t either null or undefined, but I\'ve not managed to find

Logical Operators, || or OR?

南笙酒味 提交于 2019-11-26 02:08:48
问题 I remember reading a while back in regards to logical operators that in the case of OR , using || was better than or (or vice versa). I just had to use this in my project when it came back to me but I can\'t remember which operator was recommended or if it was even true. Which is better and why? 回答1: There is no "better" but the more common one is || . They have different precedence and || would work like one would expect normally. See also: Logical operators ( the following example is taken

What does “var FOO = FOO || {}” (assign a variable or an empty object to that variable) mean in Javascript?

不想你离开。 提交于 2019-11-25 23:59:39
问题 Looking at an online source code I came across this at the top of several source files. var FOO = FOO || {}; FOO.Bar = …; But I have no idea what || {} does. I know {} is equal to new Object() and I think the || is for something like \"if it already exists use its value else use the new object. Why would I see this at the top of a source file? 回答1: Your guess as to the intent of || {} is pretty close. This particular pattern when seen at the top of files is used to create a namespace , i.e. a

JavaScript OR (||) variable assignment explanation

帅比萌擦擦* 提交于 2019-11-25 22:55:57
问题 Given this snippet of JavaScript... var a; var b = null; var c = undefined; var d = 4; var e = \'five\'; var f = a || b || c || d || e; alert(f); // 4 Can someone please explain to me what this technique is called (my best guess is in the title of this question!)? And how/why it works exactly? My understanding is that variable f will be assigned the nearest value (from left to right) of the first variable that has a value that isn\'t either null or undefined, but I\'ve not managed to find

Boolean operators && and ||

大兔子大兔子 提交于 2019-11-25 22:26:18
问题 According to the R language definition, the difference between & and && (correspondingly | and || ) is that the former is vectorized while the latter is not. According to the help text, I read the difference akin to the difference between an \"And\" and \"AndAlso\" (correspondingly \"Or\" and \"OrElse\")... Meaning: That not all evaluations if they don\'t have to be (i.e. A or B or C is always true if A is true, so stop evaluating if A is true) Could someone shed light here? Also, is there an