먼저 호이스팅에대해 정리를 하자면 호이스팅은 var라는 변수선언 해주는 변수선언문의 scope를 최상단으로 올려주는 행위 이다.
Hoisting
JavaScript Hoisting refers to the process whereby the interpreter allocates memory for variable and function declarations prior to execution of the code. Declarations that are made using var are initialized with a default value of undefined. Declarations made using let and const are not initialized as part of hoisting.
source:https://developer.mozilla.org/en-US/docs/Glossary/Hoisting
function catName(name) {
console.log("My cat's name is " + name);
}
catName("Tigger");
/*
위 코드의 결과는: "My cat's name is Tigger"
*/
catName("Chloe");
function catName(name) {
console.log("My cat's name is " + name);
}
/*
위 코드의 결과는: "My cat's name is Chloe"
*/
source:https://developer.mozilla.org/en-US/docs/Glossary/Hoisting
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).
호이스팅은 한마디로 모든 선언식을 현재의 범위에서 가장 최상단으로 올리는 것이다. (스크립트의 가장 최상단 혹은 함수의 가장 최상단으로)
To avoid bugs, always declare all variables at the beginning of every scope.
sources:https://www.w3schools.com/js/js_hoisting.asp
항상 변수는 최상단에서 선언을 하자! 이렇게 한다면 발생하게 될 수 도 있는 많은 오류들을 사전에 차단을 할 수 있다!