
호이스팅(Hoisting)
예시 )
console.log(x); // undefined
var x = 5;
console.log(x); // 5
위 코드에서 x 변수 선언이 코드 상단으로 끌어 올려지면서, 첫번째 console.log(x)에서 undefined
가 출력된다. 이는 선언만 호이스팅되었고 초기화는 나중에 발생하기 때문이다.
반면에 let과 const는 호이스팅되지만 초기화 전에는 사용 불가하여, 같은 상황에서 에러가 발생합니다.
console.log(y); // ReferenceError
let y = 5;