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