Home > Web Front-end > JS Tutorial > Destructuring assignment in Javascript

Destructuring assignment in Javascript

autoload
Release: 2021-04-15 13:55:45
Original
2113 people have browsed it

Destructuring assignment in Javascript

ES6 Before, exchanging values ​​between two variables required the introduction of a third-party variable, and ES6 introduced destructuring assignment makes code writing more concise and easier to read.

1. Exchange variable values ​​

        let a=10,b=20;
        console.log(a,b);
        [a,b]=[b,a];
        console.log(a,b);
Copy after login

2. Array destructuring

 // 等号左边是右边的模板,必须一样
        let [a,b,c] =[1,2,3];
        console.log(a,b,c);
Copy after login

Can preset the default value

        [a,b,c='JS'] =[1,2];
        console.log(a,b,c);
Copy after login

Can use merge parameters

        [a,b,...c] =[1,2,3,5,6,66];
        console.log(a,b,...c);
Copy after login

Can omit some parameters

        [,,c] =[1,2,3,5,6,66];
        console.log(c);
Copy after login

3. Object deconstruction

          ({id,name}={id:10,name:"手机"});
           console.log(id,name);
Copy after login

Recommended: "2021 js interview questions and answers (large summary)"

The above is the detailed content of Destructuring assignment in Javascript. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template