- Home
- /
- Tutorials
- /
- JavaScript Tutorial
- /
- JavaScript Hoisting
JavaScript Scope & Hoisting
JavaScript Hoisting
JavaScript hoisting moves declarations to the top of their scope before the code runs.
Only the declaration moves. The value stays exactly where you wrote it.
This is why a var can be used before its line without an error, but is undefined.
Example
Example
javascript
console.log(count);
var count = 5;
console.log(count);The first line prints undefined rather than causing an error.
The second prints 5.
What is Hoisting?
Before running your code, JavaScript scans each scope and registers every declaration it finds.
It is as if the declarations were lifted to the top, which is where the name comes from.
