JS 호이스팅 주의하기

LikeChoonsik's·2022년 1월 4일
0

JavaScript

목록 보기
3/15

호이스팅이란

선언과 할당이 분리가 되는 것, 런타임시 선언이 최상단으로 끌어올려지는 것
주로 var 쓸 때 초기화가 제대로 안돼있으면 undefined로 최상단에 끌어 올려줄 수 있는 것
선언부만 최상단 올라가는 것
ex

var hello = 0
function outer(){
consol.log(global); <-undefined나옴
global = 5;

	function inner(){
    var hello = 10;
    console.log(hello); <-10
    }
    inner();
}

이런 일은 선언,할당이 메모리공간에 선언하기전에 미리 할당을 해서 그런 것 즉 위 코드는

var hello = 0
function outer(){
var hello;
consol.log(global); <-undefined나옴
global = 5;

	function inner(){
    var hello = 10;
    console.log(hello); <-10
    }
    inner();
}

랑 같음

for반복문 쓸 때 호이스팅 때문에 let 쓴 기억 메모

const나 let을 주로 쓰자

변수 만들 때 함수표현식 추천
ex

const sum = function(){
return 1+2;
}
profile
춘식이는 너무 귀엽습니다.

0개의 댓글