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
letandconstare hoisted to the top of the block, but not initializedname = "Jane"; let name; //referenceError