[ES6+] Array destructuring(배열 비구조화)


const days = ["mon","tue","wed","thu"","fri","sat","sun"];

const mon = days[0];
const tue = days[1];
const wed = days[2];
...

console.log(mon)

이런식으로하다가는 화딱지남

const days = ["Mon","Tue","Wed","Thu"","Fri","Sat","Sun"];

const [mon, tue, wed] = days;
console.log(mon, tue, wed);
//Mon Tue Wed


만약 기본값을 정해주고 싶다면

const days = ["Mon","Tue","Wed"];

const [mon, tue, wed, thu = "Thu"] = days;
console.log(mon, tue, wed, thu);
//Mon Tue Wed Thu





노마드코더의 ‘ES6의 정석’을 듣고 정리 =)




© 2018. by sora

Powered by sora