What is hoisting in JavaScript?

Hoisting means that declarations are moved to the top of their scope before the code runs.

console.log(x);   // undefined (var is hoisted, value is not)
var x = 5;

sayHi();           // works: function declarations are fully hoisted
function sayHi() {
  console.log("Hi");
}

Output:

Tutorials Class - Output Window

undefined
Hi

let and const are hoisted too, but using them before the declaration throws a ReferenceError (the temporal dead zone). See JavaScript Scope.


Learn more about the similar topics:
Tutorials
JavaScript Variables
JavaScript Scope
JavaScript Data Types
JavaScript Type Conversion
JavaScript Operators
JavaScript Numbers and Math
Exercises & Assignments
Write a JavaScript program to add two numbers
Write a JavaScript program to swap two variables
Interview Questions & Answers
What are the data types in JavaScript?
What is the difference between == and === in JavaScript?
What is NaN in JavaScript?
What is the difference between null and undefined?
What is scope in JavaScript?
What is the difference between var, let and const?
What is hoisting in JavaScript?