지식 정리 📝

20221104 JS 스코프

엄성준 2022. 11. 4. 23:10

Scope를 우리말로 번역하면 ‘범위’라는 뜻을 가지고 있습니다.

 

JS에서 스코프는 2가지 타입이 있습니다.

 

global(전역)과 local(지역)입니다.

 

 

예제 

1
2
3
4
5
6
7
8
9
const text = 'outside';
 
{
    const text = 'inside';
    {
        console.log(text);
    }
 
}
cs

 

위의 코드인 경우 console.log(text)의 결과 값은 inside가 출력될 것입니다.

 

 

예제 

1
2
3
4
5
6
7
8
9
10
const text1 = 'outside';
 
{
    const text2 = 'inside';
    {
        console.log(text2);
    }
 
}
console.log(text2);
cs

 

위의 코드인 경우 console.log(text2)의 결과 값은 undefined가 출력될 것입니다.