JS Guide
HomeQuestionsTopicsCompaniesResources
BookmarksSearch

Built for developers preparing for JavaScript, React & TypeScript interviews.

ResourcesQuestionsSupport
HomeQuestionsSearchProgress
HomeQuestionsjavascript
PrevNext

Learn the concept

Destructuring

javascript
junior
destructuring

How do destructuring assignment and spread/rest operators work in JavaScript?

destructuring
spread
rest
es6
syntax
fundamentals
Quick Answer

Destructuring extracts values from arrays or properties from objects into distinct variables. The spread operator (...) expands iterables into individual elements, while rest (...) collects remaining elements into an array or object.

Detailed Explanation

Destructuring (ES6) unpacks values from arrays or objects:

Array destructuring:

  • Extracts by position
  • Can skip elements and set defaults

Object destructuring:

  • Extracts by property name
  • Can rename variables and set defaults

Spread operator (...):

  • Expands an iterable into individual elements
  • Used for copying arrays/objects, merging, and function arguments

Rest operator (...):

  • Same syntax but collects remaining elements
  • Used in function parameters and destructuring

Code Examples

Array and object destructuringJavaScript
// Array destructuring
const [first, second, third] = [1, 2, 3];
const [a, , c] = [1, 2, 3]; // skip second: a=1, c=3
const [x = 10, y = 20] = [1]; // defaults: x=1, y=20

// Object destructuring
const { name, age } = { name: 'Alice', age: 25 };
const { name: userName } = { name: 'Alice' }; // rename
const { role = 'user' } = {}; // default value

// Nested destructuring
const { address: { city } } = {
  address: { city: 'NYC', zip: '10001' }
};
console.log(city); // 'NYC'

// In function parameters
function greet({ name, greeting = 'Hello' }) {
  return `${greeting}, ${name}!`;
}
greet({ name: 'Alice' }); // 'Hello, Alice!'

Real-World Applications

Use Cases

React Props

Destructuring props in function components for cleaner code: function Card({ title, body })

Immutable State Updates

Using spread to create copies of state objects before modification in React and Redux

API Response Extraction

Destructuring nested API responses to extract only the relevant data fields needed by components

Mini Projects

Config Merger

beginner

Build a utility that deep-merges configuration objects using spread and destructuring

API Response Transformer

intermediate

Transform nested API responses into flat, component-ready data structures using destructuring

Industry Examples

React

useState returns an array idiomatically destructured: const [count, setCount] = useState(0)

Redux

Immutable state updates in reducers use the spread operator to shallow-copy objects and arrays before applying modifications

Resources

MDN - Destructuring assignment

docs

MDN - Spread syntax

docs

JavaScript.info - Destructuring assignment

article

Related Questions

What is the difference between map(), filter(), and reduce() array methods?

junior
arrays
Previous
What is a Single Page Application (SPA) and how does it work?
Next
What are the most commonly used string methods in JavaScript?
PrevNext