我对 JavaScript 中的结果有疑问,因为我不太了解它。为什么如果我使用这段代码,它会得到下一个结果:
var a =[1][1];
var b = [1][0];
if(a){console.log(true);}else{console.log( false);} --> returns false
if(b){console.log(true);}else{console.log(false);} --> returns true
如何解释 JavaScript 解释这些结果的确切方式?
最佳答案
其实很简单,让我们分解一下:
var a =[1][1];
分解为:
var a = [1]; //An array with the value '1' at the 0 index
a = a[1]; //assigns a the result of the 1 index, which is undefined
与 b
相同 - 但 b
使用定义的 0
索引(如 1
);
a
是 undefined
,这是假的,而 b
是 1 - 这是真值。
关于javascript - JavaScript 中 [1][1] 和 [1][0] 的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39752249/