What are arrays?
Organizing and storing data is a foundational concept of programming.
One way we organize data in real life is by making lists. Let's make one here:
New Year's 2022 Resolutions 1. Keep a journal 2. Take a falconry class 3. Learn to juggle
let newYearsResolutions = ['Keep a journal', 'Take a falconry class', 'Learn to juggle'];
Here's an array of the concepts we'll cover in these notes:
let concepts = ['creating arrays', 'array structures', 'array manipulation']
One way we can create an array is to use an array literal. An array literal creates an array by wrapping items in square brackets
. Remember, arrays can store any data type - we can also have an array that holds all the same data types or an array that holds different data types. For example:
let arrayExample = ['array example', 10, true];
The array is represented by the square brackets
and the content inside
Each content item inside an array is called an element
There are three different elements inside the array
Each element inside the array is a different data type (string, number, and boolean)
The array is saved to a variable (
arrayExample), which can be referenced later in our code
Each element in an array has a numbered position known as its index. We can access individual items using their index, which is similar to referencing an item in a list based on that item's position.
0, rather than
1. Therefore, the first item in an array will be at position
0. Let's see how we could access an element in an array:
1 2 3
citiesis an array that has three elements
We're using bracket notation,
with the index after the name of the array to access the element (see
You are also able to access individual characters in a string using bracket notation and the index. For example, you can write:
Once you have access to an element in an array, you can also update its value.
1 2 3 4
seasonsarray contained the names of the four seasons.
However, we decided that we preferred to say
'Autumn' instead of
seasons = 'Autumn'; tells our program to change the item at index 3 of the
seasons array to be
'Autumn' instead of what is already there.
const keywords. Variables with
let can be reassigned.
Variables declared with the
const keyword cannot be changed or reassigned. However, elements in an array declared with
const remain mutable. This means that we can change the contens of a
const array, but cannot reassign a new array or a different value.
1 2 3 4 5 6 7 8 9 10 11 12
let. We are only able to change elements inside an array declared with
constand will get an error if we try to change the entire array.
One of an array’s built-in properties is
length and it returns the number of items in the array. We access the
.length property just like we do with strings. Check the example below:
1 2 3 4
newYearsResolutions.lengthto the console using the following steps:
We use dot notation, chaining a period with the property name to the array, to access the
lengthproperty of the
Then we log the
newYearsResolutionto the console.
newYearsResolutionhas two elements, so
2would be logged to the console.
When we want to know how many elements are in an array, we can access the
.push() allows us to add items to the end of an array. Here we have an example of how it is used:
1 2 3 4 5 6
So how does
- We access the
push method by using dot notation, connecting
itemTracker with a period
Then we call it like a function. That's because
.push()is a function and on that JS allows us to use right on an array
.push()can take a single argument or multiple arguments separated by commas. In this case, we're adding two elements:
.push()changes, or mutates,
itemTracker. You might also see
.push()referred to as a destructive array method since it changes the initial array.
Another array method,
.pop(), removes the last item of an array.
1 2 3 4 5 6 7 8
In the example above, calling
'item 2'from the end
.pop()does not take any arguments, it simply removes the last element of an array
.pop()returns the value of the last element. In this example, we store the returned value in a variable
removedto be used later
.pop()is a method that mutates the intitial array
Additional Array Methods
There are many more array methods than just
.pop(). You can read about all of the array methods that exist on the Mozilla Developer Network (MDN) array documentation.
.push() mutate the array on which they’re called. However, there are times that we don’t want to mutate the original array and we can use non-mutating array methods. Be sure to check MDN to understand the behavior of the method you are using.
.concat() amongst many others. Using these built-in methods make it easier to do some common tasks when working with arrays.
Arrays and Functions
Throughout these notes we have gone over arrays being mutable, or changeable. Well what happens if we try to change an array inside a function? Does the array keep the change after the function call or is it scoped to inside the function?
Take a look at the following example where we call
.push() on an array inside a function. Recall, the
.push() method mutates, or changes, an array:
1 2 3 4 5 6 7 8 9
flowersarray has 3 elements
addFlower()has a parameter of
.push()to add a
addFlower()with an argument of
flowerswhich will execute the code inside
We check the value of
flowersand it now includes the
'lily'element! The array was mutated (changed)!
In conclusion, when you pass an array into a function, if the array is mutated inside the function, that change will be maintained outside the function as well. You might also see this concept explained as pass-by-reference since what we're actually passing to the function is a reference to where the variable memory is stored and changing the memory.
Earlier we noted that arrays can store other arrays. When an array contains another array it is known as a nested array. Examine the example below:
const nestedArray = [, [2, 3]];
1 2 3
nestedArraywill grab the element in index 1, which is the array
[2, 3]. Then, if we wanted to access the elements within the nested array we can chain, or add on, more bracket notation with index values.
1 2 3 4
console.log()statement, we have two bracket notations chained to
nestedArray. We know that
nestedArrayis the array
[2, 3]. Then to grab the first element from that array, we use
nestedArrayand we get the value of 2.
Arrays are created with brackets
Each item inside of an array is at a numbered position, or index, starting at
We can access one item in an array using its index, with syntax like:
We can also change an item in an array using its index, with syntax like
myArray = 'new string';.
Arrays have a
lengthproperty, which allows you to see how many items are in an array.
Arrays have their own methods, including
.pop(), which add and remove items from an array, respectively.
Arrays have many methods that perform different tasks, such as
.shift(), you can find documentation at the Mozilla Developer Network website.
Some built-in methods are mutating, meaning the method will change the array, while others are not mutating. You can always check the documentation.
Variables that contain arrays can be declared with
const. Even when declared with
const, arrays are still mutable. However, a variable declared with
constcannot be reassigned.
Arrays mutated inside of a function will keep that change even outside the function.
Arrays can be nested inside other arrays.
To access elements in nested arrays chain indices using bracket notation.