let something = "Something";
let result = null;
function foo () {
if (something === "Something") {
result = "Is something";
}
var something = "Else";
}
foo();
console.log(result)
if (result === null) {
alert("🎉");
}
let something = "Something";
let result = null;
function foo () {
if (something === "Something") {
result = "Is something";
}
// var something = "Else";
}
foo();
console.log(result)
if (result === "Is something") {
alert("🎉");
}
두 코드의 차이점을 이해하면 된다.
밑에 코드는 var something = 을 안했기 대문에 result가 잘 바뀌었고
위에 코드는 저걸 썼기 때문에 var something;부분이 맨 위로 올라가서 if 안에 있는게 실행되지 않았기 때문에 result가 그대로 null상태이다.
근데 사실 이제 var키워드는 거의 안쓰긴 하지만 그래도!
참고 글
https://www.freecodecamp.org/news/javascript-let-and-const-hoisting/
Hoisting in JavaScript with let and const – and How it Differs from var
I used to think that hoisting only happened to variables declared with var. But recently, I learned that it also happens to variables declared with let and const. I'll explain what I mean in this article. I also have a video version of this article [https:
www.freecodecamp.org