Learn about function and variable hoisting in JavaScript

JavaScript as a language is easy to get started with but difficult to master. This is partly owing to the fact that you can get up and running with a fairly complex application built with JS and not know in detail the workings of it and somewhat because once you have a functional system there is not much motivation to go back and break it down. Hoisting, Lexical Scopes, Closures, this and even IIFFE are not well understood by developers. Understanding concepts like these not only make the language interesting but also explain a lot of weird

Every building needs strong foundation to build on. Understanding variable scope in JavaScript is one of the keys to building a solid foundation.

In this article, we will be looking at the intricacies of function and variable hoisting.

There are several ways to define functions in JavaScript. We will be taking a look at the following three methods:

  • Function Declaration
  • Function Expression
  • Arrow functions.
// function declaration 
function welcome () {
console.log('Welcome to learning JavaScript');
}

// function expression 
// involves the assignment of a named or an anonymous function to a variable.
var welcome = function () {
console.log('Welcome to learning JavaScript');
}

// arrow function
var welcome = () => console.log('Welcome to learning JavaScript');

//we can simple call it with
welcome(); // Welcome to learning JavaScript

At first glance, the above ways of defining a function look the same.

However, there are subtle differences.

Let’s look at them — for the purposes of this article, we will be focusing more on function declaration and function expression.

double(5) // 10
square(2) // Uncaught ReferenceError: Cannot access 'square' before initialization
   // at :3:1
const square = function (x) {
 return x * x;
}

function double (x) {
return 2 * x;
}

As we can see, the program doesn’t work as expected.

However, if we comment out the calling of the square function at line 3 or move it below its definition, we can see that the program works as expected.

The reason for this anomaly is that we can call a function declaration before it is actually defined, but we can’t do the same for a function expression. This has to do with the JavaScript interpreter, which interprets a given script.

We made a custom demo for .

No really. Click here to check it out.

Function declarations are hoisted, while function expressions aren’t. The JavaScript engine hoists function declarations by lifting it up the current scope before actually executing the script.

As a result, the above snippet is actually interpreted as follows:

function double (x) {
return 2 * x;
}
double(5) // 10
square(2) // Uncaught ReferenceError: Cannot access 'square' before initialization
   // at :3:1
const square = function (x) {
 return x * x;
}

But the square function isn’t hoisted, which is why it is only available from the definition downwards to the rest of the program. This resulted in an error when it was called.

This is the case with function expression.

There is also another form of hoisting that happens in JavaScript, which occurs when a variable is declared using the keyword var.

Let’s look at a few examples that illustrate this:

    var language = 'javascript';
    function whichLanguage() {
            if (!language) {
                    var language = 'java';
            }
            console.log(language);
    }
    whichLanguage();

When we run the above code, we can see that our console logs out java.

If this surprises you, you’re in the right place. We’re going to take a closer look at exactly what is going on.

In the same way function declarations are hoisted, variables are declared with the keyword var.

There are a few things to note about the differences in how they are hoisted:

  1. When a function declaration is hoisted, the whole function body is moved to the top of the current scope.

  2. A variable declared using the keyword var when hoisted only moves the variable name to the top of the current scope — not the assignment.

  3. Variables declared using the keyword var are only scoped by a function, not an if block or a for loop.

  4. Function hoisting supersedes variable hoisting.

With these rules in mind, let’s see how the JavaScript engine will interpret the above code:

var language = 'javascript';
function whichLanguage() {
var language;
        if (!language) {
                language = 'java';
        }
        console.log(language);
}
whichLanguage();

As we can see, the var language was moved to the top of the current scope, thus giving it a value of undefined. That makes it enter the if blocks, which reassigns it to a value of java.

Let’s look at another example that further demonstrates this:

var name = 'gbolahan';
function myName() {
        name = 'dafe';
        return;
        function name() {}
}
myName();
alert(name);

We can deduce what the above code will produce by following the rules of how the JavaScript engine will interpret the file.

Lets see how it is interpreted:

var name = 'gbolahan';
function myName() {
function name() {} // hoisted name function
        name = 'dafe';  // name reassigned to a new value. 
        return;    
}
myName(); 
console.log(name);

gbolahan will be logged out because the name defined in the myName function is scoped by that function and is discarded after the function is execution.

This covers most of the things to consider when working with hoisting in JavaScript. There are a few exceptions to these rules, but with the introduction of ES6 you’re now able to avoid many of these caveats by using the  const and let keywords when declaring variables.

#js #javascript #web-development

Learn about function and variable hoisting in JavaScript
14.15 GEEK