This question already has answers here:
javascript multiple OR conditions in IF statement
(6 answers)
Closed 3 years ago.
if(eggsAmount < eggsMin || milkAmount < milkMin || flourAmount || FlourMin)
Does it mean whichever of these are true?
Yes, in an if statement with only OR || operators, it will simply go through each condition and as soon as it finds one that is true, the if statement resolves as true, the rest are not checked, and the code in the if block.
An if statement containing multiple OR conditions (||) has in Javascript the same behaviour descripted by Boolean Algebra: the whole evaluation is True if any of the conditions is true.
At this w3schools link you can find JS logic operators description.
Anyway the evaluation of the expression itself is not necessarily a boolean value. The expression
Res = expr1 || expr2 || ... || exprN
is evaluated as the first condition that can be evaluated as true (for example if expr1 is 4+7, Res=11.
If none of the condition can be evaluated as true the assigned value is the value contained in the last condition: Res = exprN.
(thanks to Jared Farrish, Barmar and Teemu)
Related
This question already has answers here:
Why if([]) is validated while [] == false in javascript?
(3 answers)
Closed 6 years ago.
I think in every language I know
if(a)
is the same thing as
if(a == true)
Turns out in JavaScript it isn't true, because:
if([])
Seems to act as if the condition is fulfilled, but:
if([] == true)
Does the opposite thing.
I can't really find any possible explanation, especially that this problem doesn't occur with empty string for example (which is == true, but isn't === true, same as empty array). Is this a bug in JavaScript or what?
In JavaScript, there is a concept of truthy and falsey values. if statements test the truthiness or falsiness of a given value rather than strict equality to true or false.
true is obviously truthy. false is obviously falsey. The rest can be a little tricky. MDN has possibly the clearest documentation about which values evaluate to falsey: https://developer.mozilla.org/en-US/docs/Glossary/Truthy
In this case [] is a truthy value so the condition passes and the code is executed.
This question already has answers here:
What is "x && foo()"?
(5 answers)
Closed 8 years ago.
Let's say we have this function:
loadView : function(view) {
this.view && this.view.remove();
this.view = view;
}
What does the first line do? For example if the line was this.view && this.otherView, it would return true if both view and otherView exists and false if one of them doesn't, but now there is a function being called at the end, which is confusing me.
Is the first line equivalent to:
if(this.view) {this.view.remove()}
?
it's a short circuit evaluation.
If this.view is "falsey" then this.view.remove() will not be evaluated.
So the answer to your question is yes.
It's called a guard operator. You would usually use && in this way
if(thing1 === 1 && thing2 === 2){}
You'll notice we want to check if BOTH things return true. Well the way it works is that we know that 'thing 2 === 0' will never run if the first expression (thing1 === 1) doesn't evaluate to true. Why run the second expression if they both have to be true and the first one already failed. Taking that knowledge we can now use it as a guard to not run the second expression, unless the first expression it true, or in this case truthy.
So in your code, this.view.remove() only runs if this.view is truthy.
This question already has answers here:
JavaScript OR (||) variable assignment explanation
(12 answers)
What does the construct x = x || y mean?
(12 answers)
In Javascript, what does it mean when there is a logical operator in a variable declaration? [duplicate]
Closed 8 years ago.
I've never seen the OR paramater || used outside of an if statement.
What does this line of code do?
var doc = ($iframe[0].contentWindow || $iframe[0].contentDocument).document
Is it saying making it equal to either one of those???
A || B
evaluates A first. If it is true, A is returned, and B never needs to be looked at.
If A is false, B is evaluated and returned.
For example, if you write
function (x)
{ x = x || 50
...
This would make x=50, if x is nil (or some kind of false value).
Otherwise, x would not be changed.
It is like having a default value, or a failsafe protection. If you know that the answer should never be false, then if A is false, you provide a backup value of B.
A way to get a DOM reference to the iframe's window object is to use:
contentWindow.document
Now, cause IE<8 has problems with it, a small polyfill is to use
var doc = ($iframe[0].contentWindow || $iframe[0].contentDocument).document;
// Browser you get this one ^^^ ? NO? Sh** you're IE7, go with^^
So earlyer versions of IE will skip the contentWindow cause not recognized, and thanks to the || (or) operator will follow up with the next contentDocument.
I don't have to repeat what's the OR operator cause other smart people already explained it: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Logical_Operators
This is just a short form of the ternary operator, which always returns a value depending to a statement. So, e. g.:
var fruit = "apple";
var test = fruit === "apple" ? fruit : "banana";
This sets the variable test to the value of fruit, when fruit is set to "apple". Otherwise, test will be initialized with "banana".
This question already has answers here:
How do you use the ? : (conditional) operator in JavaScript?
(20 answers)
What does ? : mean in Javascript? [duplicate]
(9 answers)
Closed 10 years ago.
I am trying to understand the Javascript/jQuery in this http://www.queness.com/post/12078/create-jquery-pinterest-pin-it-plugin plugin. The lines 20 and 22 are confusing me, the code is:
pi_media = e.data('media') ? e.data('media') : e[0].src,
pi_desc = e.attr('title') ? e.attr('title') : e.attr('alt'),
Can anyone help me out with what these lines mean in Javascript
It's the JavaScript ternary operator.
x = condition ? a : b
is equivalent to
if(condition)
x = a;
else
x = b;
Note that an assignment is not necessary. As an expression, it simply evaluates and produces a or b depending on the truth value of condition.
It's called the Ternary Operator. It means:
Evaluate the expression to the left of the ?
If the expression evaluated to true, run the first piece of code (between the ? and the :)
If the expression evaluated to false, run the second piece of code (after the :)
This is a construct common to many C-style languages.
Those are part of the ternary operator.
Basically, if the condition before the ? is evaluated to true, the expression immediately following the ? is the one which is evaluated, and otherwise the expression following the : is evaluated.
For example take the code:
var result=condition?arg1:arg2;
First the condition is evaluated.
If the evaluation results to true, then arg1 is returned and assigned to result
If the evaluation results to false, then arg2 is returned and assigned to result
This question already has an answer here:
Closed 10 years ago.
Possible Duplicate:
Javascript conditional order evaluation
How do multiple conditions in an if-statement pan out?
if( bacon && bacon == "crispy") {
self.eat(bacon);
}
If bacon doesn't exist, will it still try to check if bacon is "crispy"?
No, it uses short-circuit evaluation, so if evaluating the LHS means it doesn't need to evaluate the RHS, it won't.
This is exploited in JavaScript quite frequently...
foo = foo || "bar";
If foo is truthy (the LHS), then the condition is known to be true and foo will be assigned its value (these conditions return the last evaluated expression).
However, if foo is falsy, it will evaluate the RHS expression, and then assign that result to foo.
The && operator is a short-circuit operator in JavaScript that will prevent the rest of the statement from being evaluated if the first condition is false.