코딩 공부
JavaScript ES6+ 문법 정리 - 알아두면 유용한 기능들
새혀니
2024. 12. 4. 18:10
// 1. let과 const
let mutable = '변경 가능';
mutable = '변경 완료';
const immutable = '변경 불가';
// immutable = '에러 발생'; // TypeError
// 2. 화살표 함수
const add = (a, b) => a + b;
console.log(add(2, 3)); // 5
// 3. 템플릿 리터럴
const name = 'Se-hyun';
console.log(`Hello, ${name}!`); // Hello, Se-hyun!
// 4. 디스트럭처링
const person = { name: 'Hyeji', age: 28 };
const { name: personName, age } = person;
console.log(personName, age); // Hyeji 28
// 5. 스프레드/나머지 연산자
const arr = [1, 2, 3];
const newArr = [...arr, 4];
console.log(newArr); // [1, 2, 3, 4]
function sum(...numbers) {
return numbers.reduce((acc, num) => acc + num, 0);
}
console.log(sum(1, 2, 3, 4)); // 10