Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Easy JavaScript Part 5: Simplifying Function Hoisting

DZone's Guide to

Easy JavaScript Part 5: Simplifying Function Hoisting

In this installment, we explore function hoisting in JS and why you can't use a function expression before it is created in the code.

· Web Dev Zone
Free Resource

Learn how to build modern digital experience apps with Crafter CMS. Download this eBook now. Brought to you in partnership with Crafter Software

To understand function hoisting, let us start by considering the code listed below:

console.log(foo);
var foo = 9; 

What should the output be?

In any other programming language, the output here would be a reference error. However, in JavaScript, you will get undefined as the output. Why? Because JavaScript hoists variables at the top of the execution context. An execution context could be the function in which a variable is declared or a JavaScript file in which a variable is declared. So, let us rewrite the above code snippet using a function:

function abc() {
    console.log(foo);
    var foo = 9;
}

abc();

Here, the variable "foo" is hoisted at the top of the function abc's execution context; which means you can access foo before it is declared. To put it simply, whenever you declare a variable, the JavaScript interpreter breaks it into two statements:

  1. Declaration of a variable.
  2. Assignment.

The declaration of a variable goes at the top of the execution context, and assignment happens where the variable is created. So the above code snippet is broken into two statements as shown in the image below:

The variable foo is hoisted at the top of the execution context of the function abc, hence when you use it before its declaration, you get "undefined" as the output.

Keep in mind that a variable declared using the let statement does not get hoisted to the top of the execution context.

Now that you understand how variables are hoisted in JavaScript, let's explore function hoisting in JavaScript. In JavaScript, a function can be created in two ways:

  1. Function as a declaration.
  2. Function as an expression.
A function created as a declaration or statement is hoisted as a whole to the top of the execution context. However, a function created as an expression is hoisted like a variable.

To illustrate this, let's create a function as a statement:

foo();

function foo() {
    console.log("hello");
}

Above, if you use a function before it's created, you will get an output of hello. This is happening because a function created as a statement gets hoisted as a whole to the top of the execution context.

Whenever you create a function as a statement, you can use that function before the function is created. So if you are creating a function as a statement in line number 5, you can use that function in lines 1-4 because function statements are hoisted with the function body at the top of the execution context.

Function statements are hoisted with the function body at the top of the execution context.

Function expressions get hoisted to the top of the execution context like a variable. Let us consider the code listed below:

foo();

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

Here you are creating the function foo as an expression, so JavaScript will hoist it in the same way a normal variable is hoisted. JavaScript treats the above code as shown in the image below:

As you see in the above image, foo is getting declared as a variable at the top of the execution context, however, the assignment of a function in the variable foo is happening in line number 6 where the function as an expression was created. So, when you try to execute the code listed above, you will get the error undefined is not a function as shown in the image below:

You cannot use the function expression before it is created because only the function declaration gets hoisted at the top.

In summary:

  1. The function statement gets hoisted with the function body at the top of the execution context. You can use a function created as a statement before it's created.
  2. The function expression cannot be used before it's created. Only the declaration part gets hoisted and the assignment happens in the line where the function was created.

In the next post of this "Easy JavaScript" series, you will learn about more important concepts in JavaScript, so stay tuned.

Crafter is a modern CMS platform for building modern websites and content-rich digital experiences. Download this eBook now. Brought to you in partnership with Crafter Software.

Topics:
web dev ,javascript ,function hoisting ,functions

Published at DZone with permission of Dhananjay Kumar, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}