LearnFrenzy
Sign in Take a free test

Arrays · Predict the Output · Question 4

What sort() returns Answer, explanation & discussion

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

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

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

sort works in place and returns the same array object, so b is just another name for 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