JavaScript Destructuring Interview Questions and Answers
Practice unpacking arrays and objects, defaults, nested values, and function return data.
Frequently Asked Questions
Important questions are highlighted in the sections below; select one to jump to its answer.
- How does array destructuring bind positions?
- How does object destructuring select properties?
- What is object destructuring in JavaScript?
- What is nested object destructuring?
- Swap two variables using array destructuring.
- How does destructuring work in arrays and objects?
- How to do destructuring of a nested array?
Theoretical JavaScript Interview Questions
Destructuring is a clean ES6 syntax for pulling values out of arrays and objects without writing verbose dot-notation chains. Instead of const name = person.name; const age = person.age; you just write const { name, age } = person. You'll use this constantly - especially in function parameters, when consuming API responses, and with React hooks like useState which returns [value, setter].
Array destructuring is position-based - [a, b] picks the first and second elements regardless of name. Object destructuring is name-based - { x, y } pulls properties with those exact names. You can skip array elements with commas (const [, second] = arr), set defaults, rename, and use rest (...). Once you get the hang of it, you'll reach for destructuring constantly when working with API responses and function parameters.
Mirror the nested structure in your pattern: const [a, [b, c]] = [1, [2, 3]]. The pattern shape has to match the data shape, which can get unreadable if you go more than 2-3 levels deep. For deeply nested data, it's usually better to destructure in multiple steps rather than cramming it all into one expression. You'll hit this pattern with API responses that return coordinates or matrix data.
Object destructuring lets you pull specific properties out of an object in one clean line - much better than doing const name = person.name; const age = person.age; separately. You'll use this everywhere: in function parameters to pick out just the props you need, when consuming API responses, and when working with framework APIs. Only extract the properties you actually need - it makes the code self-documenting about what data you're working with.
The basics: const { name, age } = person extracts those properties into same-named variables. To rename, use a colon: const { name: fullName } = person. To set a default: const { age = 25 } = person. You can combine both: const { name: fullName = 'Guest' } = person. The colon syntax looks weird at first but you'll internalize it quickly - left side is the object's property name, right side is your new variable name.
Use the rest pattern (...) at the end of your destructuring to scoop up whatever's left: const [head, ...tail] = arr grabs the first element and puts the rest in an array. For objects: const { id, ...data } = response lets you separate the id while keeping everything else together. The rest element must always come last - putting it anywhere else is a syntax error. This is great for extracting a few specific properties while spreading the rest somewhere else.
Use a colon after the property name: const { userId: id } = response extracts the userId property but stores it in a variable called id. This is really useful when an API returns property names that conflict with existing variable names, or when the backend names aren't descriptive enough in your context. Remember: left side of the colon is the object's property name, right side is your new variable name.
Mirror the object's nesting in your pattern: const { address: { city, zip } } = person pulls out city and zip from person.address directly. Note that address itself won't be a variable here - only city and zip are. Nested destructuring can get hard to read if you go too deep; for complex structures, consider destructuring in multiple steps. Also worth adding defaults for nested values since you'll get a TypeError if address is undefined.
Put destructuring directly in the parameter signature: function render({ title, body, author = 'Anonymous' }) {}. This is much cleaner than accessing props.title, props.body inside the function body - the parameter list itself documents what shape of object the function expects. Always add = {} as the parameter default so the function doesn't blow up if called without arguments. You'll see this pattern constantly in React components and Express middleware.
Array destructuring assigns iterable values to variables by position. Missing positions produce `undefined` unless a default is supplied.
Object destructuring binds variables from properties with matching names, or from explicitly named properties using aliases. It reads properties from the source object rather than removing them.
A default is used when the extracted value is `undefined`, including a missing property or array slot. It does not replace `null`, `false`, zero, or an empty string.
A rest element gathers the remaining iterable items or object properties into a new array or object. It must appear last in its destructuring pattern.
Defaults are applied at the binding where the extracted value is `undefined`; they do not automatically make a missing parent object safe. Give the parent pattern a default object as well as defaults for its nested properties.
Destructuring an iterable obtains its iterator and consumes values from it, so custom iterator code may run and the iterator may be left partially consumed. A rest element consumes the remaining values.
Object rest copies remaining own, enumerable string-keyed and symbol-keyed properties into a new ordinary object after excluding already-bound keys. It does not copy inherited or non-enumerable properties.
At the start of a statement, `{ ... }` is parsed as a block rather than an object assignment pattern. Wrapping the assignment in parentheses makes its expression interpretation explicit.
JavaScript Practical and Coding Interview Questions
Problem: Swap the values of first and second with a single destructuring assignment, then log both variables.
let first = 'left';
let second = 'right';
[first, second] = [second, first];
console.log(first, second);Problem: Use array destructuring to assign the first color to `primary` and all remaining colors to `otherColors`, then log both values.
const colors = ["teal", "gold", "plum"];
const [primary, ...otherColors] = colors;
console.log(primary);
console.log(otherColors);Problem: Destructure a record with possibly missing `profile` or `settings`, default the display name to `Guest` and theme to `light`, and create a normalized object without mutating the input. Print the result.
const record = { profile: { name: "Ravi" } };
const {
profile: { name: displayName = "Guest" } = {},
settings: { theme = "light" } = {}
} = record;
const normalized = { displayName, theme };
console.log(normalized);