| [ Web Proxy ] |
| Viewing: https://developer.mozilla.org/ru/docs/Web/JavaScript/Reference/Operators/Destructuring | [Back] [Original] |
Get to know MDN better
This page was translated from English by the community. Learn more and join the MDN Web Docs community.
This feature is well established and works across many devices and browser versions. Its been available across browsers since 2016 ..
JavaScript , .
var a, b, rest;
[a, b] = [1, 2];
console.log(a); // 1
console.log(b); // 2
[a, b, ...rest] = [1, 2, 3, 4, 5];
console.log(a); // 1
console.log(b); // 2
console.log(rest); // [3, 4, 5]
({ a, b } = { a: 1, b: 2 });
console.log(a); // 1
console.log(b); // 2
({ a, b, ...rest } = { a: 1, b: 2, c: 3, d: 4 });
console.log(a); // 1
console.log(b); // 2
console.log(rest); // { c:3, d:4 }
. . .
, .
, Perl Python.
var foo = ["one", "two", "three"];
//
var one = foo[0];
var two = foo[1];
var three = foo[2];
//
var [one, two, three] = foo;
, b 1, a 3. , ( - XOR-).
var a = 1;
var b = 3;
[a, b] = [b, a];
, . , :
function f() {
return [1, 2];
}
, , . . , f() [1, 2] :
var a, b;
[a, b] = f();
console.log("A is " + a + " B is " + b);
[a, b] = f() : a 1, b 2.
:
var a = f();
console.log("A is " + a);
a 1 2.
:
function f() {
return [1, 2, 3];
}
var [a, , b] = f();
console.log("A is " + a + " B is " + b);
, a 1, b 3. 2 . ( ) . :
[, ,] = f();
exec() , , , . , :
var url = "https://developer.mozilla.org/en-US/Web/JavaScript";
var parsedURL = /^(\w+)\:\/\/([^\/]+)\/(.*)$/.exec(url);
var [, protocol, fullhost, fullpath] = parsedURL;
console.log(protocol); // "https"
var o = { p: 42, q: true };
var { p, q } = o;
console.log(p); // 42
console.log(q); // true
//
var { p: foo, q: bar } = o;
console.log(foo); // 42
console.log(bar); // true
, Add-on SDK:
const { Loader, main } = require("toolkit/loader");
var metadata = {
title: "Scratchpad",
translations: [
{
locale: "de",
localization_tags: [],
last_edit: "2014-04-14T08:43:37",
url: "/de/docs/Tools/Scratchpad",
title: "JavaScript-Umgebung",
},
],
url: "/ru/docs/Tools/Scratchpad",
};
var {
title: englishTitle,
translations: [{ title: localeTitle }],
} = metadata;
console.log(englishTitle); // "Scratchpad"
console.log(localeTitle); // "JavaScript-Umgebung"
var people = [
{
name: "Mike Smith",
family: {
mother: "Jane Smith",
father: "Harry Smith",
sister: "Samantha Smith",
},
age: 35,
},
{
name: "Tom Jones",
family: {
mother: "Norah Jones",
father: "Richard Jones",
brother: "Howard Jones",
},
age: 25,
},
];
for (var {
name: n,
family: { father: f },
} of people) {
console.log("Name: " + n + ", Father: " + f);
}
// "Name: Mike Smith, Father: Harry Smith"
// "Name: Tom Jones, Father: Richard Jones"
function userId({ id }) {
return id;
}
function whois({ displayName, fullName: { firstName: name } }) {
console.log(displayName + " is " + name);
}
var user = {
id: 42,
displayName: "jdoe",
fullName: {
firstName: "John",
lastName: "Doe",
},
};
console.log("userId: " + userId(user)); // "userId: 42"
whois(user); // "jdoe is John"
id, displayName firstName .
let key = "z";
let { [key]: foo } = { z: "bar" };
console.log(foo); // "bar"
| Specification |
|---|
| ECMAScript 2027 LanguageSpecification # sec-destructuring-assignment |
| ECMAScript 2027 LanguageSpecification # sec-destructuring-binding-patterns |
This page was last modified on 19 . 2025 . by MDN contributors.
Your blueprint for a better internet.
Portions of this content are 19982026 by individual mozilla.org contributors. Content available under a Creative Commons license.
| Web Proxy Viewer | New URL | Original Page |