Today I Learned
[Javascript] 자주 까먹는 Destucturing
떼굴펜
2024. 5. 10. 11:16
객체 Destructuring
1) 다중 속성 추출
const coffe = {
name : '커피',
price : 4000
};
const { name, price } = coffe;
console.log(name); // 커피
console.log(price); // 4000
2)함수 매개변수
function menu({name, age}) {
console.log(`오늘의 커피는 ${name}이고 가격은 ${age}원입니다.`)
}
const todayCoffe = {
name : '카페라떼',
price : 5000
};
menu(todayCoffe);
배열 Destructuring
const colors = ['red', 'orange', 'yellow', 'green'];
const [firstColor, , , forthColor ] = colors;
console.log(firstColor); // red
console.log(forthColor); // green