LearnFrenzy
Sign in Take a free test

Operators · Predict the Output · Question 17

delete on an array element Answer, explanation & discussion

  • Output
  • Hard
  • 0 comments
  • Answer explained
  • Moderated community
Question 17 Back to Exercise

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

main.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!

Post your comment

Comments appear after a quick review by our team.

Search LearnFrenzy

Esc

Quick links