Question 17
Back to Exercise
What does the following JavaScript code print (console output, as in Node.js)?
const arr = [1, 2, 3];
delete arr[1];
console.log(arr, arr.length, arr[1]);
- A [ 1, 3 ] 2 3
- B [ 1, <1 empty item>, 3 ] 3 undefined Correct
- C [ 1, undefined, 3 ] 3 undefined
- D [ 1, null, 3 ] 3 null
Correct Answer: Option B
delete removes the element but keeps the slot, creating a hole (shown by Node as an empty item). Reading a hole gives undefined; the length is unchanged.
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!