Lets’s discuss about the behaviour Hosting..
If you have minimum knowledge of how code works in JavaScript, you can easily guess the output of below code.
var channel="CodeWellTech";
console.log(channel);
and result will be
CodeWellTech
But let’s move the console.log statement one step above.. i.e place it before the declaration of channel variable like below
console.log(channel);
var channel="CodeWellTech";
Now, generally we expect console.log statement to print an error but its gives something else…
undefined
So what is happening here ?
JavaScript interpreter will see above code as
var channel;
console.log(channel);
channel="CodeWellTech";
Basically all the global declarations of variables in JavaScript will be moved to the top of the scope before execution of code and this behaviour is called Hoisting
For more detailed explanation on how JavaScript works behind the scenes
Kindly like the video if you are happy with explanation :)
Top comments (0)