JavaScript에서 호이스팅(hoisting)이란, 인터프리터가 변수와 함수의 메모리 공간을 선언 전에 미리 할당하는 것을 의미합니다. var로 선언한 변수의 경우 호이스팅 시 undefined로 변수를 초기화합니다. 반면 let과 const로 선언한 변수의 경우 호이스팅 시 변수를 초기화하지 않습니다.
var
로 선언된 변수
console.log(a); // undefined
var a = 1234;
console.log(a); // 1234
let과 const의 호이스팅
console.log(b); // ReferenceError: Cannot access 'b' before initialization
let b = 1234;
console.log(c); // ReferenceError: Cannot access 'c' before initialization
const c = 4567;
greet(); // tesst
function test() {
console.log('test');
}
함수 표현식
console.log(test); // undefined
sayHello(); // TypeError: test is not a function
var test = function() {
console.log('test');
};