1. () => {} : arrow function
기본적인 형태를 살펴보자.
const hello = () => {
console.log('hello!');
};
매개변수 하나를 추가해보자.
const hello2 = name => {
console.log('hello!', name);
};
매개변수가 하나일 때는 '()' 생략 가능하다.
매개변수가 2개 이상일 때는 다음과 같다.
const hello2 = (name, age) => {
console.log('hello!', name, age);
};
매개변수가 2개 이상일 때는 반드시 '()'를 써야 한다.
함수의 리턴하는 방법을 살펴보자.
const hello1 = (name) => {
return `hello1 &{name}`;
};
const hello2 = name => `hello2 ${name}`;
hello1과 hello2는 똑같은 함수이다. arrow function는 '{}' 생략하여 위와 같이 간단하게 나타낼 수 있다.
'JavaScript' 카테고리의 다른 글
JavaScript기초(21)_객체(1) (0) | 2021.10.15 |
---|---|
JavaScript기초(20)_함수(6) (0) | 2021.10.14 |
JavaScript기초(18)_함수(4) (0) | 2021.10.12 |
JavaScript기초(17)_함수(3) (0) | 2021.10.11 |
JavaScript기초(16)_함수(2) (0) | 2021.10.10 |
댓글