Hoisting != Hosting...

"This simple concept in JS is frequently asked about by interviewers. Although both are distinct concepts, it's the similar pronunciation of the two words that often catches people off-guard."

Hosting...you know this..?

You typically host a website. Means you rent out or buy an infrastructure or service to make your website available on web.

It simply is maintaining website and it's file structure on the web.

Hosting simply refers to service of providing server space.

Hoisting..... Wait....What..???

Keep attention to that extra 'oi' when someone sounds like Hosting. If you get that extra 'oii' sound then tell them below information .

So, 'Hoisting' is a behaviour where variables and function declarations are moved on top of their containing scope, no matter where are they written in the code. They are 'hoisted to the top of the scope' regardless of where they declared.

Simple example-

console.log(X) ;

var X=10;

the X is undefined at line one reason being it is not declared but our beloved JS won't give an error , instead it will hoist X automatically at the top, causing no error at compilation.

But keep in mind that only the declaration is hoisted, not initialization.

var x;
console.log(x);
x=5;

This is unacceptable for JS , as it can't hoist an initialization. So above code will throw you an error as 'x' is undefined.

A function declaration can also be hoisted.

somefun();

function somefun(){
       // any illogical or logical code here
}

JS will hoist a function declaration as above, if in case you forgot to do so. But again , the assignment will not hoisted.

bar();   // Typeerror
var bar=function(){
        console.log('anything you want to log');
};

The function here assigned to the variable 'bar', which will be not hoisted by JS throwing an error.


This concept is simple but asked 8/10 times during JS interview.

I hope this helps...

Thanks for reading...