함수 범위(Function Scope)
function example() {
var x = 10; // 함수 안에서 x 선언
console.log(x); // 10 출력, 사용 가능
}
console.log(x); // 접근 불가능, ReferenceError: x is not defined
블록 범위(block Scope) - 중괄호
{}
로 둘러싸인 블록(if
,for
,while
등)
if (true) {
let y = 20; // 블록 범위 변수
console.log(y); // 20
}
console.log(y); // ReferenceError: y is not defined
전역 범위(Global Scope)