LearnFrenzy
Sign in Take a free test

Arrays · Predict the Output · Question 7

Shrinking and growing length Answer, explanation & discussion

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

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

main.js
const a = [1, 2, 3];
a.length = 1;
a.length = 3;
console.log(a, a[2]);
  • A [ 1, <2 empty items> ] undefined Correct
  • B [ 1, 2, 3 ] 3
  • C [ 1, undefined, undefined ] undefined
  • D [ 1 ] undefined
Correct Answer: Option A

Shrinking deleted 2 and 3 for good. Growing length again only adds holes; reading a hole gives undefined.

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