fucntion add(a,b){
return a+b;
}
[ 화살표 함수 ]
fucntion add(a,b) => {
return a+b;
}
//아래는 같은 코드
fucntion add(a,b) => a+b;
중괄호의 여는 부분({)과 리턴(return)이 붙어있으면 생략 가능
const add = (x,y) => x+y;
//아래는 같은 코드
const add = functioin(x,y) {return x+y}; //함수이름을 지정하지 않은 '익명함수'
함수를 함수의 인자로 넘길 수 있음
//입력받은(fn) 함수에 1이라는 인자를 넣어 실행하는 함수(exec)
function exec(fn){
fn(1);
}
exec(console.log); //콘솔창에 1을 출력
exec(alert); //1을 출력하는 alert 함수
예시
const button = document.querySelector('button');
const printClicked = () => {
console.log('clicked');
}
button.addEventListener('click', printClicked);
//아래는 같은 코드 (더 줄인 코드)
const buttom = document.querySelctor('button');
button.addEventListener('click', () => {
console.log('clicked');
}