Home  >  Article  >  Web Front-end  >  Destructuring assignment in Javascript

Destructuring assignment in Javascript

autoload
autoloadOriginal
2021-04-09 17:17:542085browse

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);

2. Array destructuring

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

Can preset the default value

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

Can use merge parameters

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

Can omit some parameters

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

3. Object deconstruction

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

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!

Statement:
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