LearnFrenzy
Sign in Take a free test

Arrays · Predict the Output · Question 2

shift and unshift Answer, explanation & discussion

  • Output
  • Easy
  • 0 comments
  • Answer explained
  • Moderated community
Question 2 Back to Exercise

What does the following JavaScript code print (console output, as in Node.js)?

main.js
const queue = ["b", "c"];
const len = queue.unshift("a");
const first = queue.shift();
console.log(len, first, queue);
  • A 3 b [ 'a', 'c' ]
  • B 3 a [ 'b', 'c' ] Correct
  • C [ 'a', 'b', 'c' ] a [ 'b', 'c' ]
  • D 2 a [ 'b', 'c' ]
Correct Answer: Option B

unshift returns the new length 3; shift removes and returns the first element "a".

Discussion & Comments 0

Share your method, a shortcut, or ask about a step you didn't follow.

No comments yet. Be the first to share your solution!

Post your comment

Comments appear after a quick review by our team.

Search LearnFrenzy

Esc

Quick links