Arrays Flashcards
What is an array?
Array ares a data structure.
Way store and organize data
Why are arrays better than variables?
Variables store strings, numbers, and booleans.
Variables can store single value only– which is limiting.
What types of data can arrays hold?
Strings
Numbers
Booleans
Even Other Arrays
What are 2 practical examples where arrays can be used?
Shopping list
Online order list
What’s the syntax of an array?
variable name = [];
What symbol do you use to separate list values?
commas
Does the last value in the list of array items have a comma?
No
Why should you breakup items?
Easier to read
Easier to edit
What is zero index?
The counting starts at 0.
Rather than at 1
First item is at 0 index.
Second item is at 1 index.
Third item is at 2 index.
What symbol do you use to return values?
[]
brackets
What happens if you list an item not on the list?
Example [4]
Last item is undefined in console because nothing in index 4.
script.js contains an array of string values (the names of players in a game). Practice using array indices by accessing individual names in the players array. Start by logging the first name inside the array using the console.log() method.
First Response:
const players = [‘Toni’, ‘Anwar’, ‘Mali’, ‘Carlos’, ‘Cormac’, ‘Sariah’];
Notice the syntax:
Parenthesis
variable name
Brackets
;
Complete the code below to log the message “Arrays are awesome” to the console:
Arrays are zero-based. What does that mean?
The index of the first element inside an array is 0 (not 1).
Which of the following best describes an array?
A collection of values you can assign to a single variable.
In this array, which code snippet prints the number 1 to the JS console?
What’s a great resource for JS?
MDN
Mozilla Developer Network
What does the length property do?
returns values in array
number of items in array
How do you add elements?
Use .push to add to the end of the array.
Use .unshift to the beginning of the array
What does .push do?
Adds elements
Adds to the end of an array
What does .unshift do?
.unshift adds an element to the beginning of an array
The array assigned to the guestList variable holds a list of names. Add two new names to the end of the array using the push() method. Make sure not to type the names directly inside the guestList array 😁.
variableName.push(‘Name1’, ‘Name2’);
Now add two names to the beginning of the array using the .unshift array method
How do you remove elements from an array?
.pop and .shift methods remove items from an array
How does the .pop method remove items?
.pop takes off the last item from the array
How does the .shift method remove elements?
Takes first item off from an array
How do you remove tuna from the list?
let [new variable name] = [variableWithList].shift();
What methods help create first in, first out?
.Push and .shift method work together:
What happens if you write numbers.length given this array?
It returns the length of 5
What happens if you apply numbers.push(600)?
Adds 600 to the const numbers array
.push adds to the end of an array