Skip to the solution
MediumFrontend6 min read

What does the delete operator do?

489 views

The problem

Question presented to candidate: "If I have an array [1, 2, 3] and run delete arr[1], what does the array actually look like afterward β€” and is that different from what array.splice would have done?"

What a strong answer should cover:

  • πŸ“Œ Interview term: delete β€” removes a property entirely from an object (the key genuinely stops existing, not just becoming undefined), returning a boolean indicating success.
  • πŸ“Œ Interview term: the real, direct answer to the prompt β€” verified directly: delete arr[1] on [1, 2, 3] genuinely leaves a real hole at index 1 β€” the array's .length stays 3, and index 1 genuinely stops existing (confirmed via 1 in arr becoming false) β€” it does not shift the later elements down, which is exactly what makes it different from splice (covered in this bank's own dedicated slice/splice question), which genuinely DOES shift indices down.
  • πŸ“Œ Interview term: delete vs. setting undefined β€” a precise answer distinguishes delete obj.x (the key genuinely stops existing β€” "x" in obj becomes false) from obj.x = undefined (the key still genuinely exists, just holding the value undefined β€” "x" in obj stays true) β€” a real, verifiable distinction most candidates conflate.
  • πŸ“Œ Interview term: non-configurable properties β€” verified directly: delete on a property of a frozen object genuinely returns false silently in sloppy mode (no error, but nothing removed), while the identical call in strict mode genuinely throws a real TypeError.
  • A precise answer names that delete only ever operates on object properties, never on plain variables β€” verified directly, attempting delete on a declared variable is a genuine no-op, since variables are not object properties in the same sense (this is also why modern linters flag delete on anything but a genuine property access).

Clarifying questions expected:

  • None β€” this is a definitional/technical question; directly answering the prompt's exact array scenario (hole, not a shift) is the strong signal.

Code / implementation expected: Yes β€” reproducing the prompt's exact scenario and directly confirming the resulting hole (via .length and in) is the clearest, most convincing demonstration.

  • delete
  • objects

Try it yourself

Sketch your approach before you read the solution. That is what the interview tests.

Stuck? Get a hint.

A short nudge toward the approach. It will not give the answer away.

Solution

The solution is hidden so you can try first

6 min read Β· 9 sections

Run the code

Edit the code and run it here. Each console line with an expected value becomes a test.

JavaScriptReal proof: delete leaves a real hole on an array (unlike splice), and behaves differently for keys vs. setting undefined

Discussion

Other approaches, follow-ups and how it went in real interviews.

Sign in to join the discussion.

No comments yet. Share how you would answer it.

Question 91 of 165 in JavaScript

Keep your momentum going.

Mark this question solved to track your progress, then take the next one while it is fresh.

Up nextMedium

How would you use MutationObserver to detect when a third-party script injects new nodes into the DOM?

Continue