Basically, when JavaScript compiles all of your code, all variable declarations using var are hoisted/lifted to the top of their functional/local scope (if declared inside a function) or to the top of their global scope (if declared outside of a function) regardless of where the actual declaration has been made. This is what we mean by “hoisting”.
Functions declarations are also hoisted, but these go to the very top, so will sit above all of the variable declarations.
However, a key thing to note is that the only thing that gets moved to the top is the variable declarations , not the actual value given to the variable.
All declarations (var, let, const, function, function*, class) are "hoisted" in JavaScript.
- Variables and constants declared with let or const are not hoisted!
- JavaScript only hoists declarations, not initializations. If a variable is declared and initialized after using it, the value will be undefined.
- If a developer doesn't understand hoisting, programs may contain bugs (errors).
- In JavaScript, an undeclared variable is assigned the value undefined at execution and is also of type undefined.
- In JavaScript, a ReferenceError is thrown when trying to access a previously undeclared variable.
- Undeclared variables do not exist until code assigning them value is executed. Therefore, assigning a value to an undeclared variable implicitly creates it as a global variable when the assignment is executed. This means that, all undeclared variables are global variables.
- The scope of a variable declared with the keyword var is its current execution context.
- By enabling strict mode, we opt into a restricted variant of JavaScript that will not tolerate the usage of variables before they are declared.
'use strict';
// OR
"use strict";
- Variables declared with let and const remain uninitialized at the beginning of execution whilst variables declared with var are initialized with a value of undefined.
- A constant variable must be both declared and initialized before use.
- Function expressions, however are not hoisted.
- Function declarations are hoisted over variable declarations but not over variable assignments.
- JavaScript class declarations are hoisted. However, they remain uninitialized until evaluation. This effectively means that you have to declare a class before you can use it.
- Eliminates some silent JavaScript errors by changing them to explicit throw errors which will be spit out by the interpreter.
- Fixes mistakes that make it difficult for JavaScript engines to perform optimizations.
- Prohibits some syntax likely to be defined in future versions of JavaScript.
- To avoid bugs, always declare all variables at the beginning of every scope. Since this is how JavaScript interprets the code, it is always a good rule.
- JavaScript in strict mode does not allow variables to be used if they are not declared.
function catName(name) {
console.log("My cat's name is " + name);
}
catName("Tigger");
/*
The result of the code above is: "My cat's name is Tigger"
*/
-----
console.log(num); // Returns undefined
var num;
num = 6;
-----
num = 6;
console.log(num); // returns 6
var num;
-----
var x = 1; // Initialize x
console.log(x + " " + y); // '1 undefined'
var y = 2; // Initialize y
// The above example is implicitly understood as this:
var x; // Declare x
var y; // Declare y
// End of the hoisting.
x = 1; // Initialize x
console.log(x + " " + y); // '1 undefined'
y = 2; // Initialize y