我有一个关于数组中的空和未定义的问题
请参阅我的代码底部
const arr = []
arr[1]=1
arr[2]=2
arr[3]=3
arr[5]=5
console.log(arr[4])// console: undefined
console.log(arr)// console: [empty, 1,2,3,empty,5]
Run Code Online (Sandbox Code Playgroud)
所以我不明白两个控制台结果之间的区别
为什么 console.log(arr[4]) 未定义但 console.log(arr) 的索引 4 为空?
请帮助我,谢谢
当您读取不存在\xe2\x80\x99t 的属性时,您将获得值undefined
。那\xe2\x80\x99s标准JS。
当您记录整个数组时,\xe2\x80\x99 不会显式读取该属性,因此控制台可以帮助区分 \xe2\x80\x9chas no value\xe2\x80\x9d 和 \xe2\x80\x9cexplicitly has the undefined
value \xe2\x80\x9d。