Click here to Skip to main content
15,917,702 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
In my code I am checking whether or not to execute a block of code using if. The first expression is either true or false, but the second can also be something else. I was wondering whether there is any way to ignore the second expression in a && operator if the first expression is already true.

I hope this example helps in clearifying:

var a = true;
var b = someFunction(); // This returns an object with a boolean property, but can also undefined in some cases
if(a && b.someValue) {
 // do something
}


When b is undefined, it raises an error. However, if a is already true, is there a way not to evaluate b.someValue as that value doesn't matter anymore for the if clause? In the case a is true, the error is unnecessary as b.someValue doesn't need to be evaluated.

Thanks.
Posted

1 solution

In that case change && to || (or).
 
Share this answer
 
Comments
pimb2 25-Oct-10 14:16pm    
Thanks, I wasn't thinking of that...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900