Few months later, it was me asking the same question as I always got confused whenever these methods were mentioned.
I'm writing this article partly for myself, as a reference to fall back on on those days when everything seems not to make sense, and for every other person who's either hearing about this for the first time or actually looking for solution or clarity on the concept.
For the sake of newbies (who this article is mostly targeted at), I'll be explaining arrays briefly before we proceed in order to enable you have a clearer understanding of the context.
Let's look at arrays
Arrays are data structures which can hold values of different data types(boolean, strings, numbers) and store them inside a single variable. Values of an array are enclosed in a square bracket and each of the values are usually separated by commas .
Let a = ["name", 12, true]
Each item in an array is called
element and each element can be accessed by a numerical index, starting with
In the array above, the element at
index 0 is
name and the element at index 2 is
The element at index 1 in the above array can be accessed like this
and this will return
However, the length of the above array is 3.
Basic Operations in an array
The push operation is used to add an element to an array from the end (last element)
In the example above, an element 8 will be added to the rear end of the array
a, so we'll have
["name", 12, true, 8]
This is the opposite of push. It removes an element from the end of an array (the last element in an array). So, let's remove the number we just added to the array above.
This method adds an element to the beginning of an array. Lets add two elements to the beginning of our array
We would have our
a array to be
["age", 36, "name", 12, true]
The shift method is used to remove the first element in an array and returns the removed element. Let's remove the element
age from our array
Our current array
[36, "name",12, true]
So, now that we've covered the basics, let's look at the splice and slice methods. Before we do, feel free to have a little mental stretch.
The slice method is used to return a number of copied elements from an array.
slice() does not alter the array it is used on, rather it simply returns the copied elements while leaving the parent array untouched.
from - where the slice will start
until - where the slice will end ( the element at this position not included)
So the trick is, if we want to slice the first 2 elements from an array, our
until parameter will be 2.
Let's remove the first 2 elements from our previous array
[36, "name",12, true].
We have to specify the start
0 and the end
let newArray = a.slice(0,2)
12 (not inclusive)
newArray will be
our previous array will remain unchanged
[36, "name",12, true]
Splice is used to remove elements from an array and return the removed elements. Splice directly alters the array upon which it is called.
Let's remove 2 elements from our array
a starting from the second array (index 1) and store it in the variable
let splicedArray = a.splice(1,2); console.log(splicedArray);
This will return
a will become
If we don't specify the second parameter (numberOfElementsToBeRemoved), every element starting from the given index will be removed from the array
Splice and slice can be a bit confusing, and its okay if you still need to reference an article all the time in order to recall how they work. That's why I wrote about it, for your (and my) reference.
That's all for now.
If you enjoyed this article, leave me a cute emoji and follow me on twitter .