Loading Please Wait...
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).
In other words, a variable can be used before it has been declared.
a = 5; // Assign 5 to a
console.log(a); // Log (use) a
var a; // Declare a
Variables defined with let and const are hoisted to the top of the block, but not initialized.
Meaning: The block of code is aware of the variable, but it cannot be used until it has been declared.
Using a let variable before it is declared will result in a ReferenceError.
The variable is in a "temporal dead zone" from the start of the block until it is declared.
// With let, this will cause ReferenceError
name1 = "Lynxsia";
let name1;
// With const, this will not run
name2 = "Lynxsia";
const name2;
JavaScript only hoists declarations, not initializations.
It means we can use the variable before it's declaration but value must be assigned before it's use. Assignment after it's use will cause undefined error.
a = 10; // declare b
console.log(a); // log a
console.log(b); // log undefined
var a; // declare a
var b = 10; // declare b (hoisted) and assign 10 (no hoisted)
console.log(b); // log b
Hoisting is an unknown or overlooked behavior of JavaScript.
To avoid bugs, always declare all variables at the beginning of every scope.
JavaScript in strict mode does not allow variables to be used if they are not declared.
How you feel about this blog:
Share this blog on:
If you find any error in the turtorials, or want to share your suggestion/feedback, feel free to send us email at: info@lynxsia.com
Contact UsWe are concern with various development process like website design & development, E-commerce development, Software development, Application development, SMS & Bulk SMS Provider, PWA Development, and many more..
Copyright ©
, Lynxsia IT Solutions, All rights reserved