I have a question regarding the outcome of this in JavaScript as I don’t really understand it. Why if I use this code it gets the next result:
JavaScript
x
6
1
var a =[1][1];
2
var b = [1][0];
3
if(a){console.log(true);}else{console.log( false);} --> returns false
4
5
if(b){console.log(true);}else{console.log(false);} --> returns true
6
How to explain the exact way of how JavaScript interprets these results?
Advertisement
Answer
Pretty simple actually, lets break it down:
JavaScript
1
2
1
var a =[1][1];
2
Broken down is:
JavaScript
1
3
1
var a = [1]; //An array with the value '1' at the 0 index
2
a = a[1]; //assigns a the result of the 1 index, which is undefined
3
Same with b
– but b
uses the 0
index, which is defined (as 1
);
a
is undefined
which is falsy, and b
is 1 – which is truthy.