IT/JavaScript
왕초보 자바스크립트(코딩앙마) #11 함수표현식, 화살표함수
김숭늉이
2023. 3. 12. 17:04
728x90
1. 함수 선언문 vs 함수 표현식의 차이점?
- 함수선언문 : 어디서든 호출 할수 있음 (호이스팅!)
- 함수표현식 : 이름이 없는 함수를 만들고 변수로 선언해서 할당! 한줄씩 차례대로 읽음
호이스팅! 자바스크립트는 선언된 함수를 먼저 읽는다! => 인터프리터 언어!
2. 함수 표현식에서의 화살표 함수
-> 함수를 보다 간결하게 작성할수 있다!
showError();
let showError = function(){
console.log('error'); // uncaught Reference Error가 찍힘
}
//화살표함수
let showError = ()=> {
console.log('error');
}
showError();
const sayHello = (name) => {
const msg = `Hello, ${name}`;
console.log(msg);
}
sayHello('seungyeon'); // Hello, seungyeon으로 값 출력
const add = function(num1, num2) {
const result = num1 + num2;
return result;
}
add(2,3)
const add = (num1, num2) => (num1 + num2);
위와같이 간결하게 쓸수 있음!
728x90
반응형