
Destructuring is a concise syntax for pulling values out of arrays and objects into individual variables. This post covers array destructuring, object destructuring, default values, and the real-world patterns where this shines.
Before destructuring, extracting values from objects or arrays meant repetitive assignment statements. One variable per line, one property access per line. Destructuring collapses all of that into a single, readable declaration. Same result, a fraction of the code.
Destructuring doesn't change your data. It just gives you a cleaner way to unpack it. You describe the shape of what you want, and JavaScript extracts the values into variables for you.
javascript// Without destructuring const user = { name: "Atharv", role: "admin", age: 24 }; const name = user.name; const role = user.role; const age = user.age; // With destructuring const { name, role, age } = user;
Same outcome. The destructuring version reads like a declaration of intent: "from this object, give me these named values."
With arrays, position matters. The variable on the left maps to the element at that index on the right.
Array destructuring mapping: const [first, second, third] = [10, 20, 30]; │ │ │ │ │ │ └───────┘ └─────────┘ │ │ │ │ │ first=10 second=20 third=30
javascriptconst colors = ["red", "green", "blue"]; const [primary, secondary, tertiary] = colors; console.log(primary); // 'red' console.log(secondary); // 'green'
Skipping elements: Use an empty slot (comma with no variable name) to skip positions you don't need.
javascriptconst [, , third] = ["a", "b", "c"]; console.log(third); // 'c'
Swapping variables: One of the cleanest applications of array destructuring, no temp variable needed:
javascriptlet a = 1; let b = 2; [a, b] = [b, a]; console.log(a); // 2 console.log(b); // 1
Rest in arrays: Collect remaining elements into a new array:
javascriptconst [head, ...tail] = [1, 2, 3, 4, 5]; // head = 1 // tail = [2, 3, 4, 5]
With objects, names matter, not position. The variable name on the left must match the property name on the right.
Object destructuring extraction: const { name, age } = { name: 'Atharv', role: 'admin', age: 24 } │ │ │ │ └─────┘ └──────────────────────────┘ extracted from source object (role is ignored)
javascriptconst product = { id: 101, title: "Keyboard", price: 1499, inStock: true, }; const { title, price } = product; // id and inStock are untouched, not in scope
Renaming while destructuring: Use a colon to assign a different variable name than the property key:
javascriptconst { title: productName, price: productPrice } = product; console.log(productName); // 'Keyboard' console.log(productPrice); // 1499 // 'title' and 'price' are not defined as variables
Nested destructuring: When objects have nested structure:
javascriptconst user = { name: "Atharv", address: { city: "Pune", country: "India", }, }; const { name, address: { city }, } = user; // name = 'Atharv', city = 'Pune' // 'address' itself is not a variable here
Rest in objects: Extract specific keys and bundle the rest:
javascriptconst { password, __v, ...safeUser } = fullDocument; // safeUser has everything except password and __v
This pattern is common in API handlers where you want to strip sensitive fields before sending a response.
If a property doesn't exist or is undefined, destructuring can fall back to a default:
javascriptconst config = { theme: "dark" }; const { theme, lang = "en", fontSize = 14 } = config; // theme = 'dark' // lang = 'en' (not in config, falls back) // fontSize = 14 (not in config, falls back)
Defaults only apply when the value is undefined. A null value does not trigger the default.
javascriptconst { value = 42 } = { value: null }; console.log(value); // null — null is not undefined
You can combine renaming and defaults:
javascriptconst { role: userRole = "viewer" } = {}; // userRole = 'viewer'
This is where the pattern becomes especially useful. Instead of accessing options.timeout, options.retries, etc., you declare exactly what you need right in the function signature:
javascript// Before function connect(options) { const host = options.host; const port = options.port; const timeout = options.timeout || 3000; } // After function connect({ host, port, timeout = 3000 }) { // host, port, timeout are directly available } connect({ host: "localhost", port: 5432 });
The function signature documents its expectations. You can see at a glance what the function needs. Default values sit right next to the parameter they belong to.
Before destructuring:
javascriptfunction renderUser(user) { const name = user.name; const email = user.email; const role = user.role || "viewer"; const active = user.active; return `${name} (${email}) — ${role}`; }
After:
javascriptfunction renderUser({ name, email, role = "viewer", active }) { return `${name} (${email}) — ${role}`; }
Five lines become one. active is available if needed. Defaults are inline. The intent is clear.
key: newName.undefined. null does not trigger a default....) in destructuring to collect remaining values: array elements or object properties.Destructuring is one of those ES6 features that feels minor until you start using it consistently. Then going back to repeated obj.prop assignments starts to feel like writing longhand when you already know shorthand. Once it's part of how you write JavaScript, it just reads better.
Related posts based on tags, category, and projects
Spread and rest both use `...` but do opposite things. Spread expands an iterable into individual elements. Rest collects individual elements into a single array. This post breaks down each one clearly with real examples.
Template literals, introduced in ES6, replace messy string concatenation with a cleaner, more readable syntax. This post covers the problems they solve, how they work, and where they shine in modern JavaScript.
Variables are the foundation of every JavaScript program they let you store, label, and reuse data. This post walks you through what variables are, how to declare them, the seven primitive data types, and the three non-primitive types: objects, arrays, and functions.
Flattening arrays means collapsing nested structures into a single level. This post covers what nested arrays are, why you'd want to flatten them, and every real approach you'll encounter, including the ones that show up in interviews.