JS Startup

Challenges, Tips & Tricks & Tutorials

JavaScript Quiz – Find value after using array shift and unshift

JavaScript Quiz – Find value after using array shift and unshift

Array Shift Method –

JavaScript Array Shift Method ( Array.shift() ) is an array method which use to manipulate the array or change the array.

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.

Willing to know more about JavaScript Array Shift Method

Array UnShift Method –

JavaScript Array unShift Method ( Array.unshift() ) is an array method which use to change the array by adding a new element to array.

Just like javascript array push as new element from bottom of the array. Javascript unshift add new element at the top of the array.

And shift all remaining element to one step down in index.

If you want to know more about JavaScript Array UnShift

JavaScript Question

JavaScript Array has so many useful method in order to manipulate or change the array based on the requirement.

So, in this javascript quiz code you need to find out what should be the value of console log going to print.

First we declare a variable array using let keyword and assign a javascript array to it. JavaScript array contain 5 element add all of them are number element.

let array = [1, 2, 3, 4, 5];

Now, we use javascript array shift and passing 0 as a parameter to it.

In the next line, we use javascript array unshift and passing 6 as a parameter.

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.

Answer – 6, 5

Click To Run Code

Explanation

Now, you know the answer is 6, 5. But why this is the answer, let’s find out.

So, as we can see after declaring and assign an javascript array to array variable.

Array Shift In Action –

We are using a javascript array shift method and it removes the element from the array from the top. So, your array looks like it after using the shift method.

[2, 3, 4, 5]

But why we are passing 0 as a parameter. Just to confuse you, as javascript array shift method does not require any parameter. So no effect of 0.

Array UnShift In Action –

Now, javascript array unshift come in action. And we have used a array unshift method with passing value 6 as parameter.

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[0] value is 6 and array[4] value is 5.

I hope you understand the concept and logic behind it.

But wait, i have something more for you –

Check out our other javascript quiz – 

  1. JavaScript Quiz – What is the length of String Split Method
  2. JS Quiz – Is the calling function inside function are equal
  3. JavaScript Quiz – Find the sum of object and variable value
  4. JavaScript Quiz – What actually optional chaining operator doing with object

Conclusion

To participate in our javascript quiz or challenges, tutorial, tips & tricks make sure to join our jsstartup newsletter. So, you can able to participate in our daily challenges & learn the javascript concept.

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.

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top
17 Shares
Share via
Copy link