JS Guide
HomeQuestionsTopicsCompaniesResources
BookmarksSearch

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

ResourcesQuestionsSupport
HomeQuestionsSearchProgress
HomeTopicsjavascriptDestructuring
PrevNext
javascript
beginner
7 min read

Destructuring

destructuring
es6
fundamentals
rest
spread
syntax

Destructuring extracts values from arrays (by position) and objects (by property name) into variables. The spread operator (...) expands iterables, while the rest operator (...) collects remaining elements.

Key Points

1Array Destructuring

Extracts by position with defaults and skipping; enables variable swapping; works with any iterable

2Object Destructuring

Extracts by property name with renaming, defaults, and nesting; does not mutate the original

3Parameter Destructuring

Destructure directly in function parameters — standard pattern for React props: function Component({ title }) {}

4Spread Operator

Expands iterables into elements — merge arrays, shallow-copy objects, pass arrays as function arguments

5Rest Operator

Collects remaining elements into an array — must be last in destructuring patterns and function parameters

What You'll Learn

  • Know how destructuring works for arrays and objects including renaming and defaults
  • Understand the difference between spread (expand) and rest (collect) operators
  • Know how to destructure function parameters including React props

Deep Dive

Destructuring (ES6) provides a concise syntax to unpack values from arrays and properties from objects into distinct variables. Combined with the spread and rest operators, it's fundamental to modern JavaScript and used extensively in React.

Array Destructuring

  • Extracts values by position: const [a, b, c] = [1, 2, 3];.
  • Skip elements with commas: const [, second] = [1, 2, 3]; — second is 2.
  • Default values: const [a = 10] = []; — a is 10 when the value is undefined.
  • Swap variables without a temp: [a, b] = [b, a];.
  • Works with any iterable (arrays, strings, Sets, generators).

Object Destructuring

  • Extracts by property name: const { name, age } = person;.
  • Rename variables: const { name: fullName } = person; — creates fullName, not name.
  • Default values: const { name = 'Anonymous' } = {};.
  • Rename with default: const { name: fullName = 'Anonymous' } = {};.
  • Nested destructuring: const { address: { city } } = person; — extracts city from nested object.
  • Destructuring does not mutate the original object or array.

Function Parameter Destructuring

  • Destructure directly in parameters: function greet({ name, age }) {} — cleaner than accessing options.name.
  • Common in React: function Component({ title, children }) {} destructures props.
  • Combine with defaults: function fetch({ url, method = 'GET' } = {}) {} — the = {} prevents errors when called with no arguments.

Spread Operator (...)

  • Expands an iterable into individual elements: [...arr1, ...arr2] merges arrays.
  • Shallow-copies objects: { ...obj, newProp: value } — creates a new object with all properties plus overrides.
  • In function calls: Math.max(...numbers) spreads an array into arguments.
  • Spread creates a shallow copy — nested objects still share references.

Rest Operator (...)

  • Same syntax as spread, but collects remaining elements instead of expanding.
  • In destructuring: const [first, ...rest] = [1, 2, 3]; — rest is [2, 3].
  • In objects: const { id, ...otherProps } = obj; — otherProps contains everything except id.
  • In function parameters: function sum(...args) {} — collects all arguments into a real array.
  • Must always be the last element: const [...rest, last] = arr; is a SyntaxError.

Key Interview Distinction: Spread vs Rest Spread expands (unpacks) — used in array/object literals and function calls to spread elements out. Rest collects (packs) — used in destructuring and function parameters to gather remaining elements into an array. Same ... syntax, opposite directions. Context determines which it is.

Fun Fact

Destructuring was inspired by pattern matching in functional languages like ML and Haskell. The original ES6 proposal went through 14 revisions over 3 years. Nested destructuring like const { a: { b: { c } } } = obj compiles to multiple property accesses — making it syntactic sugar that can be surprisingly deep.

Continue Learning

Arrays

beginner

Objects & Copying

intermediate

Practice What You Learned

How do destructuring assignment and spread/rest operators work in JavaScript?
junior
destructuring
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.
Previous
Data Types
Next
DOM
PrevNext