Hoisting

Hoisting

In this article we will try to understand what is Hoisting in JavaScript .

Hoisting in JavaScript is a behavior where variable and function declarations are moved to the top of their containing scope during the compilation phase, before the code is executed. This means that you can use variables and functions before they are declared in your code.

Here are a few examples to help you understand hoisting:

  1. Hoisting with variable declarations:

     console.log(x); // Output: undefined
     var x = 5;
    

    In this example, the variable x is declared and initialized after the console.log() statement. However, due to hoisting, the variable declaration is moved to the top, resulting in undefined being printed instead of a ReferenceError.

x=5;
console.log(x); // Output: 5
var x = 5;

In the above example, we got output 5 as we can see in the output

  1. Hoisting with function declarations:
foo(); // Output: "Hello, I'm foo!"
function foo() {
  console.log("Hello, I'm foo!");
}

In this example, the function foo is called before it is declared. Despite the order in the code, hoisting moves the function declaration to the top, allowing it to be called before its actual declaration.

  1. Hoisting with variable and function declarations:
var x = 5;
console.log(x + y); // Output: NaN
var y = 10;

In this example, both the variable x and the variable y are declared and initialized. However, only the variable declarations are hoisted, not the initializations. Therefore, y is hoisted but its value is not assigned at the time of the console.log() statement, resulting in NaN (Not a Number) being printed.

It's important to note that hoisting applies only to declarations, not initializations or assignments. It is considered a best practice to declare your variables at the top of their scope to avoid confusion and unexpected behaviors caused by hoisting.

Are let and const also hoisted in JavaScript?

Yes, let and const declarations are hoisted in JavaScript, but they have a different hoisting behavior compared to var.

With var declarations, the variable is hoisted to the top of its scope and initialized with a value of undefined. This means you can access the variable before its actual declaration.

However, with let and const declarations, the hoisting process is slightly different. The variables are hoisted to the top of their block scope (the nearest curly braces {}), but they are not initialized. This means that you cannot access let and const variables before their declaration in the code. If you try to access them before their declaration, you'll get a ReferenceError.

Here's an example to illustrate the hoisting behavior of let and const:

console.log(x); // ReferenceError: x is not defined
let x = 5;

console.log(y); // ReferenceError: y is not defined
const y = 10;

In the above example, attempting to access x and y before their respective declarations will result in a ReferenceError. This is because let and const declarations are hoisted to the top of their block scope, but their initialization is not hoisted.