LearnFrenzy
Sign in Take a free test

Arrays · Predict the Output · Question 1

Return values of push and pop Answer, explanation & discussion

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

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

main.js
const a = [1, 2];
console.log(a.push(3, 4), a.pop(), a);
  • A 4 4 [ 1, 2, 3 ] Correct
  • B [ 1, 2, 3, 4 ] 4 [ 1, 2, 3 ]
  • C 4 [ 1, 2, 3 ] [ 1, 2, 3 ]
  • D 2 4 [ 1, 2, 3 ]
Correct Answer: Option A

push returns the new length (4); pop returns the removed element (4). The arguments are evaluated left to right before printing.

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