Sunday 9 December 2018

What is Function Hoisting?

In JavaScript, you can refer to a function declared later, without getting an exception, this concept is called Function hoisting.

HelloWorld.js
say_hello();

function say_hello(){
  console.log("Hello World");
}

As you see, above snippet, say_hello() method is called after the definition.

One important thing to note here is that, only function definitions are hoisted by JavaScript, not the function expressions.

HelloWorld.js
hello();

var hello = function(){
  console.log("Hello World");
}

When you ran above application, you will get below exception.

TypeError: hello is not a function



Previous                                                 Next                                                 Home

No comments:

Post a Comment