Skip to main content

Command Palette

Search for a command to run...

Hoisting

Published
•2 min read•View as Markdown
R

I am Passionate developer, love to do competitive coding and always gets my hand dirty in trying new things

HOISTING ->

Points to Remember -

  1. All declaration of both variables and functions are processed before any part of code is executed see in image- 2.Variable declaration are scanned and made Undefined.

Hositing1.png

  1. Function declaration are scanned and made Available.

Here is example of hoisting in functions-

tipper(80);
function tipper(a){
    let bill = a;
    console.log(bill+5);
}

Here by rule 3 firstly function declaration will execute first then its call. so it becomes

function tipper(a){
    let bill = a;
    console.log(bill+5);
}
tipper(80);

but you will be surprised here->

Hositing2.png

Here in code 1 firstly code declaration takes place then tipper(80) gets the Reference of function (remember this), then it make a call.

BUT in code-2 bigtipper(200) does not work it gives error that bigtipper is not a function .

Why does it give error it should work ?? (think again).

SO Actually first in code-2 the function(a) gets declared but it did not executed (both are different) so after declaration bigtipper(200) did not get any reference (unlike in code-1 in which tipper(80) gets reference after declaration ), then as var bigtipper gets its declaration has it gets reference of function(a) then it can make a call.

In short ->

  • Code - 1 works because at the time function calling, caller has reference of function.
  • Code- 2 first code give error because at the time function calling, caller has not reference of function.