Home > Article > Web Front-end > 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!