JS Startup

Challenges, Tips & Tricks & Tutorials

JavaScript Quiz – does it shift array element to the right

JavaScript Quiz – does it shift array element to the right

Shift Array Element To Right

In javascript, array is an awesome component which can contain multiple element of different-different type like array, object, number string, boolean etc. In this, we use array unshift & pop method to shift all array element from left to right.

Basic Syntax of Array Unshift and Pop

Array.unshift()
Array.pop()

For more detail above javascript array method.

JavaScript Question

In this javascript quiz, find out how does it shift the array element to the right because shift element done when last element move to first position and so on as it proceed.

First, we declare a variable array using let and assign a javascript array to it which contains only a number element.

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

In the next step, we use two array methods which are the unshift and pop method.

array.unshift(array.pop());

Now, we check the length of the array and element inside the array element using console log.

console.log(array.length); 
console.log(array);           

So, any idea what should be output of above code statement.

JavaScript Quiz Test

Here is the full javascript code snippet for you to solve.

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

array.unshift(array.pop());

console.log(array.length); // output => 🧐 ?
console.log(array);             // output => 🧐 ?

// Made with ❤️, by @jsstartup

Answer – 4, [4, 1, 2, 3]

Click To Run Code

Explanation

Now, you know the answer is 4, [4, 1, 2, 3]. But why doesn’t pop remove the element and unshift need an element to add in the array? Let me explain

First we use a array pop method which remove the last array element but in return it gives the remove element which means array pop return 4.

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

array.pop(); // 4

console.log(array); // [1, 2, 3]

But as we pass the array pop method in unshift method so value 4 added to array at start.

array.unshift(4); 

console.log(array.length); // 4
console.log(array); // [4, 1, 2, 3]

At last, That’s why we get an answer 4, [4, 1, 2, 3].

Let me know in the comment section, what you think about it.

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 – boolean return value true with string or not
  2. JavaScript Quiz – can mixed case array sort is possible
  3. JS Quiz – effect of misplaced semicolon after if statement
  4. JS Quiz – what does triple value compare 10 > 9 > 8 return

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
0 Shares
Share via
Copy link