Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
166
rated 0 times [  173] [ 7]  / answers: 1 / hits: 19289  / 13 Years ago, thu, may 5, 2011, 12:00:00

Can I count on Javascript failing immediately when one condition in an expression results to false?



f = {'a':'b'};
if (f.a !== undefined || f.a === 'b') {
// Is this OK to use, because the second condition will never be evaluated?
}

More From » javascript

 Answers
18

Yes, this is known as short circuit evaluation.



With an AND logical operator, if the first evaluates to false, then the second is never evaluated, because the condition knows enough already to be met.



With the OR logical operator, if the first one is false, it will evaluate the second one. Otherwise if the first is true it won't evaluate the second (no need to).



This is also why you see...



var a = function(b) {
b = b || 7;
}

[#92401] Tuesday, May 3, 2011, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
marib

Total Points: 596
Total Questions: 120
Total Answers: 95

Location: Nauru
Member since Thu, Feb 2, 2023
1 Year ago
;