Learning objective: Unpack values from arrays and objects with destructuring, rename and default the unpacked variables, and destructure function parameters.
Object destructuring creates variables named after properties: const { name, city } = user; is the same as writing const name = user.name; const city = user.city;.
Array destructuring unpacks by position: const [a, b] = [10, 20]; gives a = 10 and b = 20. Skip a position with an empty slot: const [, second] = list;.
Rename while unpacking with a colon: const { name: fullName } = user; creates fullName, not name.
Give a fallback with =: const { role = "viewer" } = user; uses "viewer" only when user.role is undefined.
Defaults apply only for undefined, not for null, 0 or "".
Destructuring null or undefined throws a TypeError. Parameters often default to an empty object: function f({ size = 10 } = {}).
Swapping without a temporary variable: [a, b] = [b, a];. Start a line like this with a semicolon on the previous line, or the array may join the previous expression.
Destructured parameters document which properties a function uses, which makes call sites and refactors safer than passing the whole object around unexplained.
05Definitions
Destructuring
Syntax that unpacks values from arrays or properties from objects into variables.
Object destructuring
Unpacking properties by name with const { a, b } = obj.
Array destructuring
Unpacking elements by position with const [a, b] = list.
Default value
A fallback used when the unpacked value is undefined.
Parameter destructuring
Destructuring an argument directly in a function's parameter list.
A dashboard receives a large response object but each widget needs only a few fields. Destructuring at the top of each widget function names exactly what it reads and supplies safe defaults.
6. Which signature best documents the properties a function uses?
function save(options)
function save({ id, name, overwrite = false })
function save(a, b, c, d)
function save(...args)
13Solutions and tests
Each exercise's solution appears in the exercise once you have run its tests. Solutions are one possible answer: the tests accept any implementation with the same behaviour.
Automated acceptance tests for this lesson
These are the same checks the exercises run. They are visible on purpose: client-side tests help you learn and are not secret.
Fix the destructuring: Prints title and author; Uses renaming
Unpack coordinates: Prints 12 40 0; z is 0
Format an address: Prints both addresses; Country can be given; No argument does not throw
ex-041-01: output matches the documented expected output
ex-041-02: output matches the documented expected output
ex-041-03: output matches the documented expected output
ex-041-04: output matches the documented expected output
ex-041-05: output matches the documented expected output
The lesson lives in the companion repository at curriculum/level-02-core-development/module-09-modern-javascript-syntax/lesson-041-destructuring/. Every example on this page is generated from those files.
The GitHub repository (https://github.com/lsvekis/JavaScript-Professional-Academy) has not been published yet, so these links will work once it is. Until then, use the ZIP download above.
15Summary and next steps
Object destructuring unpacks by name; array destructuring by position.
A colon renames; = supplies a default for undefined.
Destructure parameters to document what a function needs.
Give destructured parameters a default of {} when the argument is optional.
Copy a prompt into the AI assistant of your choice. Then verify what it says by running code here. No AI service is connected to this page.
Concept explanationdestructuring
Context: I am learning destructuring in JavaScript.
Task: Explain object and array destructuring, renaming and defaults with a user settings example.
Constraints: Beginner-friendly language, short runnable examples, no libraries.
Expected behaviour: I can explain the idea in my own words and predict small programs.
Testing: Finish with three quick questions for me to answer.
Code predictiondestructuring drill
Context: I have practised destructuring.
Task: Give me six destructuring snippets, including defaults with null and undefined, and ask what each prints.
Constraints: Wait for my answer before revealing each result.
Expected behaviour: Each question has exactly one correct output.
Testing: Explain any I miss with a step-by-step trace.
Debuggingdestructuring bug
Context: My destructured variable is undefined even though the object has the data.
Task: Help me find the cause with questions instead of giving me the fix.
Constraints: Do not rewrite my program.
Expected behaviour: I locate and fix the bug myself.
Testing: Suggest the console.log lines that would reveal the problem.
Exercise generationdestructuring practice
Context: I finished the destructuring exercises.
Task: Create three exercises that use destructuring on API-style responses.
Constraints: Fictional data and exact expected output.
Expected behaviour: Each exercise has tests I can run.
Testing: Put the solutions in a separate hidden section.
Code reviewdestructuring review
Context: Here is my code that uses destructuring.
Task: Review my functions and suggest where destructured parameters would make them clearer.
Constraints: Explain each suggestion briefly and keep behaviour identical.
Expected behaviour: The code is clearer and has no hidden mutation or edge-case bug.
Testing: List the changes in order of importance.