Removing an Element from an Array
You have already learned how to remove the last element from an array. You just use the array pop()
method!
Calling pop()
on an array returns the value of the element removed. This value can be stored into a variable for future use. Here we store this value into a variable called elem
and then print it out.
When the above code is run, the result is:
Being able to remove the last elements from an array is useful, but what if you wanted to remove elements from within the array? What if you wanted to remove multiple elements from within an array?
Array Remove Method
To remove an element from within an array, you use the remove(index#)
method.
The code above prints:
Since arrays are numbered starting at index 0, index 2 in the array above is the value 12
. Thus, the value 12
gets removed from inside the array.
Just like with pop()
, remove(#)
returns the value of the element removed. You can store this value and do things with it.
The code above prints:
Array Splice Method
You can also use the splice(index#, amountToRemove)
method to remove elements from within an array. It requires two arguments, index#
and amountToRemove
. The first argument is the starting index to remove. The second argument is the amount of elements to remove from the given starting index. For example:
The code above prints:
Notice that arr.splice(2, 1)
does the exact same thing as arr.remove(2)
because we are only splicing out one element!
However, you are not limited to removing just one element! You can remove multiple elements with splicing. The second argument determines the amount of elements to remove. Below, we are removing 4 elements rather than just 1.
After running the code, the program prints:
Last updated
Was this helpful?