array in javascript

All typed array are zero initialized.

./code/array2.js
1// how to iterate an array
2const a = [ 10, 20, 30 ];
3a.forEach((val, index) => {console.log(index, val)});
4/*
50 10
61 20
72 30
8 */
./code/array.js
 1let a = [ 1, 2, 3 ];
 2let b = a; // a reference
 3b[0] = 100;
 4console.log(a[0] === 100); // true
 5
 6a = [ 1, 2, 3 ];
 7let c = [];
 8for (let i = 0; i < a.length; ++i) {
 9  c[i] = a[i]; // note: no need to pre-allocate space for c
10}
11console.log(c); // [1, 2, 3]
12
13c[10] = 20;
14console.log(c);        // [1, 2, 3, <7 empty items>, 20]
15console.log(c[4]);     // undefined
16console.log(c[5]);     // undefined
17console.log(c.length); // 11, note is is 1 larger than 10
18
19let s = "";
20for (let i in c) {
21  s += ` ${i}`;
22}
23// note that indexes with undefined are not printed
24console.log(s); // 0 1 2 10
25
26s = "";
27for (let i of c) {
28  s += ` ${i}`;
29}
30
31// there are seven undefined below
32console.log(s); // 1 2 3 undefined undefined .... 20
33
34a = [ 1, 2, 3 ]
35c = Array.from(a); // return a copy of the array a
36c[0] = 100;
37console.log(a[0] == 1); // true
38
39function equalArrays(a, b) {
40  if (a === b) {
41    return true;
42  }
43
44  if (a.length != b.length) {
45    return false;
46  }
47
48  for (let i = 0; i < a.length; i++) {
49    if (a[i] != b[i]) {
50      return false;
51    }
52  }
53
54  return true;
55}
56
57console.log(equalArrays(a, c));           // false
58console.log(equalArrays(a, a));           // true
59console.log(equalArrays(a, [ 1, 2, 3 ])); // true
60c[0] = 1;
61console.log(equalArrays(a, c)); // true
62
63a = [ 1, 2, 3 ];
64a.push(4); // use push to append()
65a.push(5);
66console.log(a);   // [1, 2, 3, 4, 5]
67a.push(6, 'ten'); // push two elements
68console.log(a);   // [1, 2, 3, 4, 5, 6, 'ten']