Arrays Flashcards
What is an array?
An object that lets us store many values under one variable name, in the form of an ordered list
With what keyword should we declare an array? Why?
const
What are 3 different ways to create and express an array?
(1) const name = [item1, item2 …];
can also format as:
const name = [
item1,
item2,
…
];
(2) let name = new Array();
(3) define an empty array, then assign values to each index
const cars = [];
cars[0]= “Saab”;
cars[1]= “Volvo”;
cars[2]= “BMW”;
When using the new
in-built array constructor, what’s something we should be careful of?
If we use new
for one single value that is a number e.g. new Array(40), it will try to create 40 items instead of a single item with the value of 40
How do you access an array - esp the first and last element?
array[index number]
for first element: array[0]
for last element: array[array.length - 1]
How do you change an array element?
array[index] = new value
Identify two ways to add array elements
(1) .push()
e.g. array.push(new element)
(2) .length
e.g. fruits[fruits.length] ="Lemon";
How can we make sure an element is an array?
Use Array.isArray() method
Using typeof will return an object, because an array is a type of object