7. Arrays

Follow along with code examples here!

Table of Contents

Array Basics

  • Arrays are lists of data (order matters). Values in an array are called elements of the array.

  • Arrays use square brackets [] to encapsulate the data

  • Arrays are a type of object

  • Like strings arrays also...

    • have indexes starting at 0

    • use bracket notation to access individual elements: array[index]

    • have a .length property that returns the number of elements in the array

    • have many of the same read-only methods like slice, indexOf and includes

  • Unlike strings, arrays are mutable.

Challenge: This function can verify whether or not a given array arr contains a given value value. For each numbered comment below, add a comment explaining the logic of the function!

Check out this example

Arrays Are Mutable

Strings have read-only methods like toUpperCase() and slice() that make a copy of the string but don't change the original string.

Unlike strings, arrays let you mutate them directly (change their contents without reassignment).

We can even modify the .length of an Array to empty them:

Question: How are we allowed to modify the array if it is stored in a const variable?

const prevents us from reassigning the variable. Notice that we never reassign endLetters! The variable still references the same array, we're just changing the contents of the array.

Array Methods for Adding and/or Removing Values

Since arrays are mutable, they not only have read-only methods like slice, indexOf, and includes, they also have methods for adding to and removing from the array.

These methods let you add values to an array

  • arr.push(value) — adds a given value to the end of an array, increasing the length by 1

  • arr.unshift(value) — adds a given value to the front of an array, increasing the length by 1

These methods let you remove values from an array

  • arr.pop() — removes a given value from the end of an array, reducing the length by 1

  • arr.shift() — removes a given value from the front of an array, reducing the length by 1

Splice lets you add and remove values to and from an array all at once!

  • arr.splice(index, qtyToRemove, valuesToAdd) — removes or replaces elements in an array and/or adds new elements to an array

Reference vs. Primitive Values

Arrays and Objects are considered reference types. Let's see why.

How Reference Types (Arrays and Objects) are Stored in Memory

When a variable is created, a chunk of your computer's memory is assigned to hold some data. Primitive values are small enough to be stored in memory as-is. Arrays and objects (a.k.a "reference types") however can grow to be any size and therefore cannot be contained within a single memory slot.

Instead, the data in arrays and objects are stored in an area called the heap and a reference to their heap address is stored in the variable instead.

As a result, we can mutate the contents of an array without reassigning the variable because the variable doesn't hold the array, it holds a reference to the array!

Mutability vs. Reassignment

Consider the code below, what will the value of nums and clone be after the program runs?

Both nums and clone will hold the values [1,20,3]. Why?

Again, this is because arrays are reference types because:

  • When we assign clone = nums, the "value" of nums is the reference to the array [1,2,3], not the array itself. So, clone also holds a reference to the exact same array

  • When we mutate clone[1], we are mutating the array referenced by clone which is the same array referenced by nums. So, both nums and clone return the mutated array.

The same thing happens when we invoke a function and provide a reference type as an argument:

It is impossible for this behavior to occur when dealing with primitive values like strings, numbers, and booleans because they are immutable.

In this example, even though it looks like we're mutating the value y, we are NOT. We're reassigning y to store a completely different value (11).

Impure and Pure Functions

Functions are considered impure functions if they:

  1. Produce different outputs when given the same inputs

  2. Produce side effects (like mutating incoming values)

The functions below are impure functions:

Making Copies of Arrays to Make Pure Functions with the Spread Syntax

Functions that accept Arrays and modify them are impure. To make a function that modifies an Array pure, we need to make a copy of it first. The most effective way to do this is using the spread syntax: [...arr]

Copying Array Challenge

Make this impure array function pure!

Advanced Array Syntax

2D Arrays

An Array can contain other Arrays! When the inner arrays contain values, we call this a "2-Dimensional (2D)" Array or a "matrix".

The matrix below has 2 columns and 3 rows:

When accessing a 2D array, the first index references the "row" and the second index references the "column"

Destructuring Assignment and Rest Operator

The destructuring assignment syntax is a JavaScript expression that makes it possible to "unpack" values from arrays (or properties from objects) into distinct variables.

The syntax uses [] on the left side of an assignment operator = to define which values to unpack from the Array on the right side (the "source variable");

In the example above, we only unpacked the first 3 values of coordinates.

If we want to, we can use the "rest" operator (...rest) to store the remaining unpacked values in a variable:

Last updated