分割代入(既定値)

(式と演算子)
https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment

分割代入とは、配列から値を取り出して、あるいはオブジェクトからプロパティを取り出して別個の変数に代入することを可能にする JavaScript の式である。

let a, b, rest;
[a, b] = [10, 20];

console.log(a); // 10
console.log(b); // 20

[a, b, ...rest] = [10, 20, 30, 40, 50];

console.log(rest);
// Array [30, 40, 50]

それぞれの分解されたプロパティは、既定値 を持つことができる。既定値は、プロパティが存在しないか、値が undefined である場合に使用される。プロパティが値 null を持つ場合は使用されない。

const [a=5, b=7] = [1];
console.log(a, b);
//1 7
inserted by FC2 system