[JavaScript] Hoisting

김_리트리버·2021년 8월 18일
0

javascript

목록 보기
1/2

Hoisting is JavaScript's default behavior of moving all declarations to the top of the current scope (to the top of the current script or the current function).

정의

모든 선언들을 선언의 scope 안에서 최상위로 끌어올리는 것

사용하는 이유

변수나 함수나 사용되기 직전에 선언, 할당하는게 가독성 측면이나 실수를 방지하는 측면에서 좋은데, 컴퓨터는 위에서 아래로만 code 를 읽기 때문에 예전 C 언어에서는 변수나 함수를 scope 최상단에 선언해야 했음

hoisting 을 통해 변수 선언 위치를 scope 최상단에 위치시키는 것을 개발자가 하지 않아도 되게 함

즉 hoisting 을 통해 사람눈에 보기좋은 code 를 컴퓨터가 보기 좋게 바꿔주기 때문에 hoisting 을 사용

주의

JavaScript only hoists declarations, not the initializations.

선언만 hoisting 하지 할당은 안함

참고

https://www.w3schools.com/js/js_hoisting.asp

https://developer.mozilla.org/ko/docs/Glossary/Hoisting

https://www.geeksforgeeks.org/javascript-hoisting/

profile
web-developer

0개의 댓글