JS hoisting

function tomato(){
    alert('Tomato one');
}

tomato();

function tomato(){
    alert('Tomato two');
}

tomato();

Will alert „Tomato two“ two times because the second „tomato()“ function will be hoisted above the fitrts one.

For functions – yes, but not for function expressions. There won’t be hoisting.

var tomato = function tomato(){
    alert('Tomato one');
}

tomato();

var tomato = function tomato(){
    alert('Tomato two');
}

tomato();

Will alert „Tomato one“
And if you call tomato() again under the second function tomato()… „Tomato one“ and „Tomato two“ will be alerted.

So, if we have function expression – NO HOISTING,

But if we have declared function – it will be hoisted because it is so called „executable portion of the code“.

Hoisting only moves the declaration, the assignments are left in place.

Вашият коментар

Вашият имейл адрес няма да бъде публикуван. Задължителните полета са отбелязани с *