let empty = []; // An array with no elements 没有元素的数组
let primes = [2, 3, 5, 7, 11]; // An array with 5 numeric elements 有 5 个数值元素的数组
let misc = [ 1.1, true, "a", ]; // 3 elements of various types + trailing comma 3 种不同类型的元素,最后还有一个逗号
let base = 1024;
let table = [base, base+1, base+2, base+3];
let b = [[1, {x: 1, y: 2}], [2, {x: 3, y: 4}]];
let count = [1,,3]; // Elements at indexes 0 and 2. No element at index 1 索引 0 和 2 有元素,索引 1 没有元素
let undefs = [,,]; // An array with no elements but a length of 2 这个数组没有元素但长度为 2
let a = new Array();
let a = new Array(10);
let a = new Array(5, 4, 3, 2, 1, "testing, testing");
Array.of() // => []; returns empty array with no arguments 返回没有参数的空数组
Array.of(10) // => [10]; can create arrays with a single numeric argument 可以创建只有一个数值元素的数组
Array.of(1,2,3) // => [1, 2, 3]
let copy = Array.from(original);
let truearray = Array.from(arraylike);
let a = ["world"]; // Start with a one-element array 先创建包含一个元素的数组
let value = a[0]; // Read element 0 读取元素 0
a[1] = 3.14; // Write element 1 写入元素 1
let i = 2;
a[i] = 3; // Write element 2 写入元素 2
a[i + 1] = "hello"; // Write element 3 写入元素 3
a[a[i]] = a[0]; // Read elements 0 and 2, write element 3 读取元素 0 和 2,写入元素 3
a.length // => 4
A sparse array is one in which the elements do not have contiguous indexes starting at 0. 稀疏数组就是其元素没有从 0 开始的索引的数组。
let a = new Array(5); // No elements, but a.length is 5. 没有元素,但 a.length 是 5
a = []; // Create an array with no elements and length = 0. 创建一个空数组,此时 length = 0
a[1000] = 0; // Assignment adds one element but sets length to 1001. 赋值增加了一个元素,但 length 变成了 1001
let a1 = [,]; // This array has no elements and length 1 这个数组没有元素,但 length 是 1
let a2 = [undefined]; // This array has one undefined element 这个数组有一个 undefined 元素
0 in a1 // => false: a1 has no element with index 0 a1 在索引 0 没有元素
0 in a2 // => true: a2 has the undefined value at index 0 a2 在索引 0 有 undefined 值
[].length // => 0: the array has no elements 数组没有元素
["a","b","c"].length // => 3: highest index is 2, length is 3 最高索引为 2,length 值为 3