학습
구조 분해 할당
용사지둥
2021. 8. 5. 22:46
구조 분해 할당
- 배열이나 객체를 변수로 분해할 수 있게 해주는 특별한 문법
let fruits = ['apple', 'orange', 'strawberry', 'mango']
let [redFruit,,,yellow] = fruits;
console.log(redFruit) //'apple'
console.log(yellow) //'mango'
let obj = {name: 'jidoong', level: 12, 직업: 'yongsa'}
let {name, level, 직업} = obj;
console.log(name) //'jidoong'
console.log(level) //12
console.log(직업) //'yongsa'
//기본값 할당
let [firstName = "what?", lastName = "good"] = ["so"]
console.log(firstName) //'so'
console.log(lastName) //'good'