Skip to solution
easyFrontend

What is destructuring?

198 views
01

Understand the problem

Question presented to candidate: "You need to swap the values of two variables, and separately, pull a deeply nested value plus a couple of top-level ones out of a config object, giving one of them a different local name and a fallback if it's missing. Walk me through how destructuring handles both, and what happens if you try to destructure something like null?"

What a strong answer should cover:

  • 📌 Interview term: destructuring — a real, single-expression syntax for unpacking array elements (by position) or object properties (by name) directly into variables, supporting defaults, renaming, skipping, and nested patterns all at once.
  • 📌 Verified, not assumed — directly answering the swap sub-question: real array destructuring genuinely swapped two variables' values with no temporary third variable at all — [x, y] = [y, x] genuinely worked, confirmed by real output.
  • 📌 Verified, not assumed — directly answering the nested/renamed/default sub-question: a real, single destructuring statement genuinely pulled a renamed top-level property AND a nested property out of a config-shaped object in one expression; a separate, real default-plus-rename combination on a genuinely missing key correctly fell back to the real default value.
  • A precise answer names rest in destructuring (const [head, ...tail] = arr, const { a, ...rest } = obj) as collecting the genuinely remaining elements/properties — a real, distinct but related capability, covered together with spread in this bank's own dedicated question.
  • 📌 Interview term: destructuring null/undefined genuinely throws — directly answering the prompt's exact question: a real attempt to destructure null genuinely threw a real, specific TypeError ("Cannot destructure property ... as it is null"), confirmed directly — not a silent undefined fallback.

Clarifying questions expected:

  • "Does the actual source value being destructured ever genuinely come back as null or undefined (from an API response, say), which would need a real guard or default at the OUTER level before destructuring, verified above as otherwise throwing?"
  • "Does function-parameter destructuring need its own top-level default (like function f({...} = {})) for the case where the function is genuinely called with no argument at all?" — a real, common, easy-to-miss requirement verified directly in this answer's own example.

Code / implementation expected: Yes — a real no-temp-variable swap, a real nested-plus-renamed-plus-defaulted object destructure, and a real, direct proof that destructuring null throws a specific error, are the concrete, convincing proof of exactly how the syntax behaves end to end.

destructuring
02

Attempt it yourself

Sketch your approach before reading the solution — that's what interviews test.

Nudge consolestandby

Stuck? Beam a request up — the console returns a conceptual nudge that guides your logic without spoiling the implementation.

03

Study the solution

Target Audience: Engineers preparing for JavaScript ES6+ syntax interviews. Difficulty: Easy

How to read this doc: Concepts are explained in plain language first, then tagged with 📌 Interview term:. The real swap, the real nested/renamed/defaulted extraction, and the real thrown error on `n

Solution ready — 2 min read

Classified // press E to declassify

04

Run the code

JSReal destructuring: a no-temp-variable swap, nested/renamed/defaulted object extraction, and the real thrown error on null
05

Join the discussion

Discussion (0)

Sign in to join the discussion.

No responses yet. Be the first to share what you think.

Transmission complete // awaiting log

KEEP THE
STREAK ALIVE.

Dossier 37 of 165 decoded in the JavaScript track. One more won't hurt.

Back to track