const numbers = [1, 2, 3];
numbers[10] = 11;
console.log(numbers);
[1, 2, 3, 7 x null, 11]
[1, 2, 3, 11]
[1, 2, 3, 7 x empty, 11]
SyntaxError
When you set a value to an element in an array that exceeds the length of the array, JavaScript creates something called "empty slots". These actually have the value of undefined
, but you will see something like:
当你为数组中的元素设置一个超过数组长度的值时,JavaScript
会创建一个名为“空插槽”的东西。这些位置的值实际上是undefined
,但你会看到类似的东西:
[1, 2, 3, 7 x empty, 11]
这取决于你运行它的位置(每个浏览器有可能不同)。