Example 1console.log(productID);
Error
|
Example 2console.log(productID1);
var productID1 = "Car"; Error
|
Example 3var total = price * quantity;
var price = 2.99; var quantity = 10; console.log(total); Error NaN var total = price * quantity; console.log(total); console.log(price + " before declaration"); var price = 2.99; console.log(price + " after declaration"); console.log(quantity + " before declaration"); var quantity = 10; console.log(price + " after declaration"); console.log(total); You will see NaN here because the vars quantity and price are underneath the total variable which contains a mathematical equation that requires execution. |
Example 1 - function expression (does not work) |
Example 1 - function expression (it works) |
Example 2 - function declaration |
showProduct();
var showProduct = function () { console.log('This is not working'); }; This will not work because in function expression we have to declare var before the function gets called |
var showProduct1 = function() {
console.log("HEY, I am working"); } showProduct1(); |
showMessage();
function showMessage() { console.log("This is functional hoist!"); } This works because the function gets executed on 2nd pass and there is no need to look for declaration variables. |