Arrays Flashcards
What is an ordered collection of values?
An Array
What is an element of an array?
Each value in the array
What are array indexes?
It is the numeric position of the element in the array
What does it mean to JavaScript arrays are untyped?
An array element may be of any type, and different elements of the same array may be of different types.
Can array elements be objects or other arrays?
Yes
What are some properties of Arrays in JavaScript?
- Ordered collection of values
- Specialized form of Object (array indexes are like properties)
- Arrays are dynamic: grow or shrink as needed
- JavaScript arrays are untyped
- Array elements may be objects or other array
- Are zero-based and use 32-bit indexes (Highest posible index 2↑32 - 2)
What does it mean that JavaScript Arrays may be sparse?
The elements not need to have contiguous indexes, and ther may be gaps.
let array = [10,2,,5,1]
What is the .length?
5
let array = [,,]
What is the .length?
2
What are the four ways to create arrays?
- Array literals
- … spread operator
- Array() constructor
- Array.of() and Array.from() factory methods
What is an Array literal?
comma-separated list of array elements within square brackets
~~~
let empty = [];
let primes = [2,3,4,7,11];
~~~
Is the following code valid? Why?
~~~
let base = 1024;
let table = [base, base + 1, base +2, base + 3];
~~~
It is valid because array literal values need NOT to be constants.
What does the following code print?
let array = [10,2,,5,1] console.log(array[2]);
undefined
What is missing the make the following code valid?
let a = [1, 2, 3]; let b = [0, ?, 4]; // b == [1, 2, 3, 4]
The spread operator in line 2
~~~
let a = [1, 2, 3];
let b = [0, …a, 4]; // notice …a
// b == [1, 2, 3, 4]
~~~
How to create a copy of the following array using the spread operator?
let original = [1, 2, 3];
let original = [1, 2, 3]; let copy = [...original];
Modifying the copy does not change the original