LearnFrenzy
Sign in Take a free test

Arrays · Predict the Output · Question 9

Writing past the end Answer, explanation & discussion

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

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

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

length becomes the highest index + 1, and the indexes in between are holes.

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