1 min read

JavaScript Hoisting

Hoisting is (to many developers) an unknown or overlooked behavior of JavaScript.If a developer doesn't understand hoisting, programs may contain bugs (errors).
JavaScript Hoisting

Hoisting is JavaScript's default behavior of moving declarations to the top.

In JavaScript, a variable can be declared after it has been used.In other words; a variable can be used before it has been declared.

Example 1:

`x =` ; // Assign 5 to x
elem = document.getElementById("demo"); // Find an element
elem.innerHTML = x;    // Display x in the element
var x; // Declare x


Variables defined with let and const are hoisted to the top of the block, but not initialized.

Example 2: This will result in a ReferenceError.

carName = "Volvo";
let carName;

Example 3: Using a const variable before it is declared, is a syntax error, so the code will simply not run.

carName = "Volvo";
const carName;

JavaScript Hoisting

#JavaScript #Hoisting #Variables #Probyto #ProbytoAI

Subscribe and follow us for latest news in Data Science and Machine learning and stay updated!

Facebook: https://facebook.com/probyto
Twitter: https://twitter.com/probyto
LinkedIn: https://linkedin.com/company/probyto
Instagram: https://instagram.com/probyto