eajni IT 초보사전 💦💦

[JavaScript] 자바스크립트 기초 (호이스팅)

2023-07-28

Hoisting VS. Declaration

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)

    Declarations are Hoisted

  • a variable can be used before it has been declared ```js x = 5; 

elem = document.getElementById(“demo”);
elem.innerHTML = x;   

var x; 


## Initializations are Not Hoisted
```js
var x = 5; 
  
elem = document.getElementById("demo");   
elem.innerHTML = x + " " + y; //y is undifinded          
  
var y = 7;

Const and Let

  • Variables defined with let and const are hoisted to the top of the block, but not initialized
    name = "Jane";  
    let name;   //referenceError
    

Comments

Content