Arrays
Table of Contents
Slides
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 dataArrays are a type of object
Like strings arrays also...
have indexes starting at
0use bracket notation to access individual elements:
array[index]have a
.lengthproperty that returns the number of elements in the arrayhave many of the same read-only methods like
slice,indexOfandincludes
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!
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:
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 1arr.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 1arr.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 RAM is assigned to hold some data. Primitive values are small enough to be stored in memory. Arrays and objects (a.k.a "reference types") however can grow to be any size and therefore cannot be contained within a single memory address.
So, instead, they 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!
Pass by Reference
when you copy an array or object from one variable to another, or you pass an array or object into a function, the reference is being passed, not the array or object itself.
Because variables store immutable raw data values (not references), it is said that we "pass by value" when we pass strings, numbers, or booleans into a function. There is no way to mutate the incoming value in a way that would affect the variable in the outer scope.
Impure and Pure Functions
Functions are considered impure functions if they:
Produce different outputs when given the same inputs
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