Reputation: 139
I'm so wonder about the condition statement in Javascript. I have two example as below:
var myObj = {};
console.log(myObj.pro != null && myObj.pro.val != null ? 0 : 1);
console.log(myObj.pro.val != null && myObj.pro != null ? 0 : 1);
The second line will print a value, but the third line will raise an exception. Is the condition checked from left to right? How is it checked when we have a lot of condition operators such as OR, NOT, (), AND in the same line. Which one will be checked first? Please correct me if I'm misunderstood.
Update: I add more complex example:
var !(!A || B && C && D) ? 0 : 1;
Upvotes: 2
Views: 398
Reputation: 809
var myObj = {};
console.log(myObj.pro != null && (code which is not read)? 0 : 1);
In you first case, the code is executed something like above , myObj.pro !=null is false , thus without running anything else it gives 1
console.log(myObj.pro.val != null && myObj.pro != null ? 0 : 1);
In you second case when it tries to read pro.val (pro is undefined thus don't have any properties to access) gives error. When you try to access a property of undefined this error is returned.Example
var obj;
console.log(obj) // undefined;
obj.key // Cannot read property 'key' of undefined(obj is undefined in this case)
Upvotes: 1
Reputation: 959
For the second line console.log(myObj.pro != null && myObj.pro.val != null ? 0 : 1);
, myObj.pro
evaluates to undefined
which is then compared to null
and that returns false
. We know that in an AND comparison, Javascript ignores the right hand side of the AND when the left hand side evaluates to false
. This reasonable behaviour prevents the interpreter from encountering the exception-bound right hand side code which is essentially undefined.val
The third line just goes ahead and evaluates undefined.val
which throws an exception and halts interpretation.
If you are interested in knowing about precedence, take a look at this MSDN table
Upvotes: 3
Reputation: 781
What is happening here is like this,
console.log(myObj.pro != null && myObj.pro.val != null ? 0 : 1);
your &&
separates 2 conditions like this (myObj.pro != null
)[consider 'A'] && (myObj.pro.val != null ? 0 : 1
)[consider 'B'].
console.log(A && B ? 0 : 1);
so A
gives null, &&
don't consider going to B
, since it got false value from A
.
console.log(B && A ? 0 : 1);
but here you are trying to check value of undefined
(in this case myObj.pro
is undefined
) that throws the error.
hope this helps you.
Update
var !(!A || B && C && D) ? 0 : 1;
assuming that all A,B,C,D have true values, here we go from left to right step by step:
!A => false
false || B => true
true && C => true
true && D => true
!(true) => false
var false ? 0 : 1;
you get 0
Upvotes: 0
Reputation: 2590
var myObj = {};
Note: myObj.pro is undefined.
console.log(myObj.pro.val != null && myObj.pro != null ? 0 : 1);
thorws exeption beacause it can't check myObj.pro.val
first because myObj.pro
is undefined. (cant find .val of undefined)
its like Looking for undefined.val
thats why you've got en exeption.
Upvotes: 0
Reputation: 172
try this
var myObj = {};
console.log(myObj.pro != null && myObj.pro.val != null ? 0 : 1);
console.log(myObj.pro != null && myObj.pro.val != null && myObj.pro != null ? 0 : 1);
Upvotes: 0
Reputation: 589
Its not about the order. In your example, myObj = {};
Hence it does not have property pro (That is myObj.pro is null) Which causes the below statements first condition myObj.pro != null to fail. Since the condition used is && the next condition is not tested and it returns 1.
In the next statement the first condition is myObj.pro.val != null. Since the object myObj does not have property pro and val which is being accessed, its throwing an exception.
Upvotes: 0
Reputation: 5707
A B A AND B
0 0 0
0 1 0
1 0 0
1 1 1
let's mark it like this
A = myObj.pro != null
B = myObj.pro.val != null
console.log(A && B != null ? 0 : 1);
because A is false we arent checking B here because false && something always be false
that's way you don't get an exception because B not even tested
in the second term
console.log(B && A != null ? 0 : 1);
B is tested before A and thorws exception because myObj.pro is undefined and you traying to access myObj.pro.val
Upvotes: 3
Reputation: 79
I believe it will get executed from left to right. But you have an Undefined property val
. It will start with myObj.pro
which will be undefined
. Then undefined.val
will definitely raise the exception here. In your second line, that didn't raise an exception because it didn't pass the first Conditional statement
Upvotes: 0