01.11.01. JS Operators - Destructuring Assignment - MDN PDF
01.11.01. JS Operators - Destructuring Assignment - MDN PDF
Web technology for developers JavaScript JavaScript reference Expressions and operators Destructuring assignment Change language
Table of contents
Syntax
Destructuring assignment
Description The destructuring assignment syntax is a JavaScript expression that makes it possible to unpack values
Examples from arrays, or properties from objects, into distinct variables.
Specifications
Browser compatibility
See also
Related Topics
JavaScript
Tutorials:
Complete beginners
JavaScript Guide
Intermediate
Advanced
References:
Built-in objects
Addition (+)
Assignment (=)
await Syntax
Bitwise AND assignment (&=)
Exponentiation (**)
const x = [1, 2, 3, 4, 5];
function* expression
Function expression
The destructuring assignment uses similar syntax, but on the left-hand side of the assignment to define
Greater than or equal (>=) what values to unpack from the sourced variable.
Inequality (!=)
Similarly, you can destructure arrays on the left-hand side of the assignment
instanceof
Object initializer
Assignment separate from declaration
Operator precedence A variable can be assigned its value via destructuring, separate from the variable's declaration.
Classes Without destructuring assignment, swapping two values requires a temporary variable (or, in some low-
level languages, the XOR-swap trick ).
Errors
Misc let a = 1;
let b = 3;
In this example, f() returns the values [1, 2] as its output, which can be parsed in a single line with
destructuring.
function f() {
return [1, 2];
}
let a, b;
[a, b] = f();
console.log(a); // 1
console.log(b); // 2
function f() {
return [1, 2, 3];
}
[,,] = f();
Be aware that a SyntaxError will be thrown if a trailing comma is used on the right-hand side of a rest
element:
function parseProtocol(url) {
const parsedURL = /^(\w+)\:\/\/([^\/]+)\/(.*)$/.exec(url);
if (!parsedURL) {
return false;
}
console.log(parsedURL);
// ["https://developer.mozilla.org/en-US/docs/Web/JavaScript",
"https", "developer.mozilla.org", "en-US/docs/Web/JavaScript"]
console.log(parseProtocol('https://developer.mozilla.org/en-US/docs/Web/JavaScript'));
// "https"
Object destructuring
Basic assignment
const user = {
id: 42,
is_verified: true
};
console.log(id); // 42
console.log(is_verified); // true
let a, b;
Note: The parentheses ( ... ) around the assignment statement are required when using object literal
destructuring assignment without a declaration.
{a, b} = {a: 1, b: 2} is not valid stand-alone syntax, as the {a, b} on the left-hand side is
considered a block and not an object literal.
Your ( ... ) expression needs to be preceded by a semicolon or it may be used to execute a function on
the previous line.
console.log(foo); // 42
console.log(bar); // true
Here, for example, const {p: foo} = o takes from the object o the property named p and assigns it to
a local variable named foo .
Default values
A variable can be assigned a default, in the case that the value unpacked from the object is undefined .
console.log(a); // 3
console.log(b); // 5
console.log(aa); // 3
console.log(bb); // 5
function userId({id}) {
return id;
}
console.log(userId(user)); // 42
console.log(whois(user)); // "jdoe is John"
This unpacks the id , displayName and firstName from the user object and prints them.
drawChart({
coords: {x: 18, y: 30},
radius: 30
});
Note: In the function signature for drawChart above, the destructured left-hand side is assigned to an
empty object literal on the right-hand side: {size = 'big', coords = {x: 0, y: 0}, radius = 25}
= {} . You could have also written the function without the right-hand side assignment. However, if you
leave out the right-hand side assignment, the function will look for at least one argument to be supplied
when invoked, whereas in its current form, you can call drawChart() without supplying any parameters.
The current design is useful if you want to be able to call the function without supplying any parameters, the
other can be useful when you want to ensure an object is passed to the function.
let {
title: englishTitle, // rename
translations: [
{
title: localeTitle, // rename
},
],
} = metadata;
console.log(englishTitle); // "Scratchpad"
console.log(localeTitle); // "JavaScript-Umgebung"
console.log(foo); // "bar"
console.log(fizzBuzz); // "true"
const props = [
{ id: 1, name: 'Fizz'},
{ id: 2, name: 'Buzz'},
{ id: 3, name: 'FizzBuzz'}
];
console.log(name); // "FizzBuzz"
Specifications
Specification
Browser compatibility
Report problems with this compatibility data on GitHub
Firefox for Android
Samsung Internet
WebView Android
Internet Explorer
Chrome Android
Opera Android
Safari on iOS
Chrome
Node.js
Firefox
Opera
Safari
Edge
41 41
Destructuring assignment 49 14 No 36 8 49 49 36 8 5.0 6.0.0
Computed property
49 14 41 No 36 10 49 49 41 36 10 5.0 6.0.0
names
See also
Assignment operators
"ES6 in Depth: Destructuring" on hacks.mozilla.org
Feedback Firefox
MDN Mozilla
© 2005-2021 Mozilla and individual contributors. Content is available under these licenses.