[JS] Scope - Local & Global (변수 선언 범위)

Janet·2022년 8월 30일
0

JavaScript

목록 보기
6/26
  • {}을 기준으로 밖에서 선언한 변수(Global variable)는 {}안에서나 밖에서나 상관없이 사용 가능하다.
  • 하지만 {}안에서 선언한 변수(Local variable)는 {}밖에서 사용 불가하다.
// Local scope & Global scope

let globalMsg = 'global'; // global variable 선언
function printMsg() {
    let localMsg = 'local'; // {}안에서 local variable 선언
    console.log(localMsg); // local
    console.log(globalMsg); // global
}
console.log(globalMsg); // global
console.log(localMsg); // error: localMsg is not defined
profile
😸

0개의 댓글