Local scope

haesoo·2021년 4월 16일
0

Local scope

let globalMessage = 'global'; 				//global variable
function printMessage() {
    let message = 'hello';
    console.log(message); 				// local variable
    console.log(globalmessage);
    function printAnother() {
    	console.log(message);
        let childMessage = 'hello';
    }
    // 만약 여기서 console.log(childMessage); 를 출력하게 되면 error 발생함.
}
printMessage();

scope: 블럭 안에서 변수를 선언하게 되면 안에서만 출력이 가능.
하위 블럭에서 상위 블럭의 변수를 console.log 하게 되면 그 값이 출력되지만,
상위 블럭에서 하위 블럭 안의 변수는 console.log 할 수 없음.

'밖에서는 안이 보이지 않고 안에서만 밖을 볼 수 있다!' 라고 기억!

profile
후론트, 숨참고 딥 다이브

0개의 댓글