Arrays Flashcards
How do we create an array?
let arrayName = []
What can we store in an array?
- Any valid data type
- objects
- arrays
- functions
How do we find the value of an item in an array?
we query the array with arrayName[n]
n is the index of the item in the array
What happens if we query an array with an index larger than the array holds
we get ‘undefined’ as the result
Can we get items from the end of an array?
Yes, by using negative indices we can query from the end of an array.
How could target the end of an array?
arrayName[arrayName.length - 1]
How can we assign a value to an item in an array
arrayName[n] = someValue
will create or reassign the value of an item in an array
if you insert a item value deep into an array, what will the intervening values be set to?
undefined
(or empty in google chrome)
What method could we use to find the index of a value in an array?
We could use the .indexOf method to find the index of a value.
Describe the indexOf method syntax on an array
arrayName.indexOf(value)
What data types will indexOf work on?
indexOf works on primitive data types
If a value is not present in an array, what does indexOf return
indexOf will return -1 if a value does not exist in an array.
How can we add a value to the front of an array?
we can use the unshift method to add a value to the front of an array.
How can we add a value to the end of an array?
We can use the push method to add a value to the end of an array.
What are two methods used for adding values to an array
- .unshift - to the front of an array
- .push - to the end of an array
How can we remove a value from the start of an array?
We can use the .shift method to remove a value from the start of an array
What is the syntax for using the shift method on an array
arrayName.shift()
What method can we use to remove a value from the end of an array
We can use the pop method to remove a value from the end of an array
Name two methods we can use to remove values from an array
- .shift - remove from front of array
- .pop - remove from end of array