Destructuring (JavaScript)

Destructuring is a syntax for pulling values out of arrays or properties out of objects into separate variables in one line.

JavaScript

Instead of accessing items one by one, you write a pattern that mirrors the array or object and the matching values are assigned automatically.

It is widely used to unpack function arguments, API responses, and module imports.

Syntax

const { a, b } = obj;   const [x, y] = arr;

Examples

JavaScript

const user = { name: "Kai", role: "admin" };
const { name, role } = user;
console.log(name, role);

Output:

Kai admin

JavaScript

const [first, second] = [10, 20];
console.log(first, second);

Output:

10 20

Learn it in a lesson

See also

Back to the Dictionary