Hoisting in javascript

Hello devs, Today i am going to show you an important javascript behavior about functions and variables declaration.

Pure javascript functions can be called before you define it. This is called Hoisting in javascript.

Example1: See my below function with example.


callHost();

function callHost()
{	
  alert("Hello this is function calling.");
}

Example2: Now if you use variables before initializing it then javascript will treat them as undefined.

See my below example.

alert(a)
const a = 5;

So for functions actually you can hoist them according to your need.

Blog Catagory : JAVASCRIPT