Question 9
Back to Exercise
What does the following JavaScript code print (console output, as in Node.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!