Hoisting in JavaScript. Code examples open

Hoisting in JavaScript. Code examples

Hoisting represents the process of accessing variables before they are defined.
If the variable is defined after being used directly, no error will occur, since the first pass the compiler already knows all the variables.

Consider an example:

    console.log(foo); //will get undefined because the variable is allocated after its use
    var foo = "Alex";

The function will behave differently:

    display();//we can call the function first before allocating it, but it will execute successfully

    function display() {
        console.log("Hello");
    }

But this will not work when the function is assigned as a variable:

    display();

    var display = function () {
        console.log("Hello");
    }

In this case, the compiler will assign the value underfined to the display variable and this will result in a TypeError: display is not a function

0

More

Leave a Reply

Your email address will not be published. Required fields are marked *

How many?: 22 + 22

lil-code© | 2022 - 2024
Go Top
Authorization
*
*
Registration
*
*
*
*
Password generation