Arrays Flashcards
push Method
.push() allows us to add items to the end of an array.
pop Method
.pop() method removes the last item of an array.
Review Arrays
Arrays are lists and are a way to store data in JavaScript.
Arrays are created with brackets [].
Each item inside of an array is at a numbered position, starting at 0.
We can access one item in an array using its numbered position, with syntax like: myArray[0].
We can also change an item in an array using its numbered position, with syntax like myArray[0] = “new string”;
Arrays have a length property, which allows you to see how many items are in an array.
Arrays have their own methods, including .push() and .pop(), which add and remove items from an array, respectively.
Arrays have many other methods that perform different functions, such as .slice() and .shift(). You can read the documentation for any array method on the Mozilla Developer Network website.
Variables that contain arrays can be declared with let or const. Even when declared with const, arrays are still mutable; they can be changed. However, a variable declared with const cannot be reassigned.
What will be logged to the console when we run the code below?
let cities = [‘Chicago’, ‘San Fransisco’, ‘New York’];
console.log(cities[3]);
What will be logged to the console when we run the code below?
New York
Chicago
undefined
undefined
Which of the methods below does not change the array it is called on?
Which of the methods below does not change the array it is called on?
.pop()
.slice()
.shift()
.slice()
What will the errands array contain after we execute the code below?
let errands = [‘Go to the bank’, ‘Pick up dry cleaning’, ‘Go grocery shopping’];
errands.shift();
What will the errands array contain after we execute the code below?
[‘Pick up dry cleaning’, ‘Go grocery shopping’, ‘Go to the bank’];
[‘Go to the bank’, ‘Pick up dry cleaning’];
[‘Pick up dry cleaning’, ‘Go grocery shopping’];
[‘Go grocery shopping’, ‘Go to the bank’, ‘Pick up dry cleaning’];
[‘Pick up dry cleaning’, ‘Go grocery shopping’];
How could you access the second item, ‘Lion’, in the code below?
let animalArray = [‘Sloth’, ‘Lion’, ‘Chipmunk’];
How could you access the second item, ‘Lion’, in the code below?
animalArray[1]
animalArray[2]
animalArray[‘Lion’];
animalArray[1]
What is the result of the following code?
const fruits = [‘Apples’, ‘Oranges’, ‘Pears’, ‘Mangos’];
fruits[2] = ‘Bananas’;
console.log(fruits);
What is the result of the following code?
[‘Apples’, ‘Oranges’, ‘Bananas’, ‘Mangos’]
[‘Apples’, ‘Oranges’, ‘Pears’, ‘Bananas’, ‘Mangos’]
[‘Apples’, ‘Bananas’, ‘Pears’, ‘Mangos’]
[‘Apples’, ‘Oranges’, ‘Bananas’, ‘Pears’, ‘Mangos’]
[‘Apples’, ‘Oranges’, ‘Bananas’, ‘Mangos’]
What will be logged to the console when we run the code below?
let myArray = [‘item 0’, ‘item 1’, ‘item 2’];
myArray.push(‘item 3’);
myArray.pop();
console.log(myArray);
What will be logged to the console when we run the code below?
[‘item 0’, ‘item 1’, ‘item 2’, ‘item 3’]
[‘item 1’, ‘item 2’, ‘item 3’]
[‘item 0’, ‘item 1’, ‘item 2’]
[‘item 0’, ‘item 1’, ‘item 2’]
How can you find how many items are within an array?
How can you find how many items are within an array?
myArray.findLength
length(myArray)
myArray.length
myArray.length
What will happen after running the following code?
const countries = [‘Japan’, ‘Denmark’, ‘Mexico’, ‘Morocco’];
countries.shift();
console.log(countries);
countries = [‘England’, ‘Mozambique’, ‘Cambodia’, ‘Peru’];
console.log(countries);
What will happen after running the following code?
One array will be logged to the console followed by an error message
[‘Japan’, ‘Denmark’, ‘Mexico’];
TypeError: Assignment to constant variable
Two arrays will be logged to the console:
[‘Japan’, ‘Denmark’, ‘Mexico’];
[‘England’, ‘Mozambique’, ‘Cambodia’, ‘Peru’];
One array will be logged to the console followed by an error message
[‘Denmark’, ‘Mexico’, ‘Morocco’];
TypeError: Assignment to constant variable
Two arrays will be logged to the console:
[‘Denmark’, ‘Mexico’, ‘Morocco’];
[‘England’, ‘Mozambique’, ‘Cambodia’, ‘Peru’];
One array will be logged to the console followed by an error message
[‘Denmark’, ‘Mexico’, ‘Morocco’];
TypeError: Assignment to constant variable
What is the correct syntax for an array?
What is the correct syntax for an array?
let myArray = {‘Rappel into a cave’, ‘Take a falconry class’, ‘Learn to juggle’};
let myArray = [‘Rappel into a cave’, ‘Take a falconry class’, ‘Learn to juggle’];
let myArray = [‘Rappel into a cave’; ‘Take a falconry class’; ‘Learn to juggle’];
let myArray = ‘Rappel into a cave’, ‘Take a falconry class’, ‘Learn to juggle’;
let myArray = [‘Rappel into a cave’, ‘Take a falconry class’, ‘Learn to juggle’];
What is the purpose of an array?
What is the purpose of an array?
To organize data at lettered positions.
To organize data into key/value pairs.
To organize data as a list.
To organize data as a list.