Arrays
Arrays, as mentioned before are any ordered set of values separated by commas and enclosed in square brackets ([]
).
When creating an array, it is possible to split it into multiple lines. Type the opening square bracket and hit enter
; your cursor will go to the next line. Then, enter a value, put a comma, and hit ctrl
+ enter
(or cmd
+ return
on mac). The console will go to the next line. Keep doing this until you have entered all the values needed, then navigate to the closing square bracket and hit enter
to execute the line.
As with strings, you can access a particular element in an array using square bracket indexes:
These indexes can be chained to access particular elements in nested arrays:
You can also reassign values in an array in this way:
The length of an array is accessible through its length
property:
Adding values to an array
To add values to the end of an array, there are 2 ways. The first way is to assign values using indexing.
In general, arr[arr.length] = x
will add x
to the end of the array.
It is possible to use the wrong index an accidentally reassign a value. It is also possible to pass in an index beyond the length of an array. In this case, JavaScript will create "gaps" in the array filled with the value undefined
:
The second way to add arrays prevents any chance of using the wrong index, and involves the push
method.
The method takes in one argument and pushes it to the back of the array. It also returns the new length of the array after insertion. push
is a variadic function, so you can push multiple values at once:
There is no easy way to insert elements into a specific position in the array. One way is to copy over the elements into a new array, making sure to insert the new value you want to at the right index, and then use the new array. Another way is to use the splice
method, as detailed in this StackOverflow post.
Removing values from an array
The best way to remove elements from an array is to use the pop
method. It is a nullary function that removes the last element in the array and returns the element it removed.
Again, it is not easy to remove a specific element; the splice
and indexOf
methods will be needed.
Searching for an element
To check if an element exists in an array, the indexOf
method is useful. It returns -1 if the element is not present, otherwise it returns the index of the element.
indexOf
will not work when passing in another array as an argument:
This is because indexOf
uses the ===
operator, which returns false
for two arrays even if they have the same elements unless they refer to the same object:
Array sliceing
To get a particular portion of an array, you can use the slice
method. It takes in a start
and end
and returns all the elements from the start
index to just before the end
index:
Omitting the end
value will slice till the end of the array:
If the end
value is greater than or equal to the start
value then it returns an empty array:
If only one argument k
is provided and the argument is negative, then the last |k|
elements are returned:
Array sorting
There are two ways to sort an array. The first is the sort
method. It takes no arguments, has no return value and sorts the array it is called on:
The second way is to use the toSorted
method. This does not change the original array and instead returns a new sorted array:
Both these methods sort the elements of the array according to their natural ascending order as defined by JavaScript. To sort an array in descending order, you can call one of the sort
or toSorted
methods and then reverse the array.
Reversing an array
Like with sorting, there are two ways to reverse the ordering of elements in an array. The first is the reverse
method, which works like the sort
method (i.e. it changes the original array):
The second way is to use the toReversed
method which is similar to the toSorted
method (i.e. it does not modify the original array and returns a new one instead):
To sort an array in descending order, combine the sorting and reversing methods:
Iterating through an array
A for
or while
loop can be used to iterate through the elements of an array and perform an action. For this next bit let's try to go through the elements of an array nums
and add the squares of each element to a new array. Below is the code for this:
But there is a better way of doing this. Arrays have a forEach
method that takes in a unary function and applies it to every element in the array. It is common to use lambda expressions and anonymous functions for this. The above code can be shortened to:
If the unary function passed in to forEach
returns any value, the value is ignored and "lost" i.e. cannot be used by any code you write
More methods
There is a full list of array methods here; this section would be far too long if I tried covering them here.
Next steps
Now we're done covering the basics of JavaScript. The next section will move on to HTML, followed by CSS, and then back to JavaScript where we'll combine the 3 to make a functional frontend.
Last updated