JavaScript arrays
collecting several objects into one variable
Definition of an array
We can think of an array as a list or collection of related objects - in JavaScript, this would look like:
With JavaScript, we can include objects of different types (such as strings and numbers) into the same array, while other programming languages might not allow us to do this!
Creating arrays
The example we have just seen shows how to create an array in JavaScript:
We can even create an empty array like this:
Adding elements to the array :push
() and unshift()
push
() and unshift()Then, we can add elements individually by using the arrayname[index]
bracket notation:
Note that the first element of an array is 0
, not 1
!
Also, in JavaScript, array element indices must be a number! (You cannot use a string as an index!)
We can also add elements to the end of array with the push
method:
To add elements at the beginning of an array, we can use the unshift
method:
Accessing array objects
We can also use the arrayname[index]
bracket notation to access array objects, e.g.:
Updating array objects
Updating an array object is basically the same as re-creating it:
Converting arrays to strings : toString()
and join()
toString()
and join()
If we have an array and wish to print out the elements, we have to convert it to a string with a simple method called toString()
:
We can also use the join()
method to convert an array into a string and then join each element with any arbitrary string:
By default, join()
, without an argument, will return a String
list that has no spaces after commas (pretty much the same as toString()
!)
Finding the length of an array : length
length
To find the length (number of elements) of an array, we have another simple property called length
:
Looping through arrays : for
for
Most programming courses will teach the for
loop as a way to iterate, or move, through an array:
Taking the last element of an array : pop()
pop()
We can use the pop()
method to get the last element of an array:
Taking the first element of an array : shift()
shift()
We can use the shift()
method to get the first element of an array:
Multi-dimensional arrays
We can think of a two-dimensional array as just a table (or in algebra, a matrix) with rows and columns!
A chessboard is a good example of a matrix:
We would then access an element using double square bracket notation like this:
The former set of square brackets would denote the row and the latter set the column!
Destructuring arrays
In a technique called destructuring, we can "take apart" an array and place the parts into variables - for example, in our chessboard
example from above, we can use the following syntax:
Note that we do not have to include all of the elements in the array! We can even skip elements by placing a blank space in between commas like this:
We can even use the spread operator, ...variablename
, to include the "rest" of the elements in an array:
This would return a "sub-array" with any element that wasn't individually destructured!
The element with the spread operator must appear as the last element of the destructuring so:
...will not work! A SyntaxError
will occur:
Searching arrays : indexOf()
indexOf()
Much like a String,
we can use the Array version of the built-inindexOf()
method to see if a value exists inside an array:
The indexOf()
method also comes with an optional parameter, which allows us to search from (and including) a specific index in the array:
Adding and removing elements from the middle of an array with splice()
splice()
Not to confuse with slice()
, the splice()
method allows us to "do surgery" on an array by removing items from it, and then adding items to the array:
The original array will change (mutate)! Thus, we can see what happens to myArray
by logging it to the console immediately!
Meanwhile, the splice()
method will return the removed elements of the original array
In general, the splice()
method consists of:
Checking if an array has a certain value : includes()
includes()
Also very similar to the String
's built-in method, the Array's version of includes()
returns a Boolean that specifies whether an array has an element with a certain value:
Copying arrays with concat()
, slice()
, ...spread
concat()
, slice()
, ...spread
An obvious way to copy an array would be to do something like this:
However, any change in the copiedArray
would result in a change in the original array:
We call this phenomenon deep copying: a change in one copy results in a change in the original!
To perform shallow copying of an array (i.e., one where each copy is actually its own thing), we would use some other methods:
Using concat()
concat()
Using slice()
slice()
Using the spread ...
operator
...
operatorLast updated