LearnFrenzy
Sign in Take a free test

Arrays · Predict the Output · Question 13

reverse() and the original Answer, explanation & discussion

  • Output
  • Medium
  • 0 comments
  • Answer explained
  • Moderated community
Question 13 Back to Exercise

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

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

reverse changes a in place and returns a itself, so pushing to r pushes to a. Use toReversed() for a copy.

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