Home > Web Front-end > JS Tutorial > How Does JavaScript's Object Bracket Notation Enable Destructuring Assignment?

How Does JavaScript's Object Bracket Notation Enable Destructuring Assignment?

Barbara Streisand
Release: 2025-01-02 14:24:41
Original
725 people have browsed it

How Does JavaScript's Object Bracket Notation Enable Destructuring Assignment?

Object Bracket Notation ({ Navigation } =) on the Left Side of Assignment

In JavaScript, the object bracket notation ({ Navigation } =) on the left side of an assignment is a syntax that allows for destructuring assignment. This feature, introduced in the ES2015 standard, enables the extraction of specific properties from an object into variables.

Object Destructuring

The object destructuring syntax allows the extraction of properties using the same syntax as object literal creation. For example, the following code assigns the p and q properties of an object o to the variables p and q:

var o = {p: 42, q: true};
var {p, q} = o;

console.log(p); // 42
console.log(q); // true
Copy after login

You can also assign new variable names to the extracted properties:

var {p: foo, q: bar} = o;

console.log(foo); // 42
console.log(bar); // true
Copy after login

Array Destructuring

Destructuring can also be applied to arrays, simplifying the assignment of individual elements to variables. Consider the following array foo:

var foo = ["one", "two", "three"];
Copy after login

Without destructuring, you would assign elements to variables as follows:

var one   = foo[0];
var two   = foo[1];
var three = foo[2];
Copy after login

With destructuring, you can achieve the same result more concisely:

var [one, two, three] = foo;
Copy after login

The above is the detailed content of How Does JavaScript's Object Bracket Notation Enable Destructuring Assignment?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template