Array Shift Method –
Array shift method remove the array element from top of the array. It’s similar to array pop method but instead of removing element from bottom / last of the array.
Array UnShift Method –
And shift all remaining element to one step down in index.
let array = [1, 2, 3, 4, 5];
Finally, at the last statement we are printing an two console log for checking the value of array index 0 and 4.
So, can you guess what is the output of it.
Now, you know the answer is 6, 5. But why this is the answer, let’s find out.
Array Shift In Action –
[2, 3, 4, 5]
Array UnShift In Action –
So, it add at the top or starting of the array. Now, your array finally look like it after array unshift method.
[6, 2, 3, 4, 5]
It’s just like you remove the one element from the top or start of the array and new element at the top of the array.
That’s why we get an array value is 6 and array value is 5.
I hope you understand the concept and logic behind it.
But wait, i have something more for you –
- JS Quiz – Is the calling function inside function are equal
And last but not the least, don’t forget to like, comment and share. It gives us a morale boost to remain to continue. Also, join our Facebook Page
If you have any questions, please feel free to ask me in the comment section and also let me know if you have any suggestions. As suggestions are always welcome.