Hoisting in Javascript

Hoisting is a behavior in JavaScript that allows variables and functions declared in a code block to be accessible before they are defined or declared. This concept can be confusing for beginners and experienced developers alike, as it can lead to unexpected behavior and bugs in your code.

In JavaScript, variables declared with the “var” keyword are hoisted to the top of their scope, while variables declared with “let” and “const” are hoisted to the top of their scope, but they are not initialized until the line where they are declared. This means that variables declared with “var” will have a value of undefined until they are assigned a value, while “let” and “const” will remain uninitialized until they are declared.

For example, consider the following code:

javascript
console.log(a); // undefined
var a = 10;

In this code, the variable “a” is declared with the “var” keyword and is hoisted to the top of the scope. When we log the value of “a” to the console, it will output “undefined”, as the variable has been hoisted but has not yet been assigned a value.

Function declarations are also hoisted in JavaScript, meaning that they can be called before they are defined in the code. For example:

scss
sayHello(); // "Hello World"
function sayHello() {
console.log("Hello World");
}

In this code, the function “sayHello” is declared with the function keyword and is hoisted to the top of the scope, allowing it to be called before it is defined.

It’s important to note that while hoisting can make your code more readable and easier to write, it can also lead to unexpected behavior and bugs if not used properly. For example, consider the following code:

javascript
console.log(a); // 10
var a = 10;

In this code, the variable “a” is declared with the “var” keyword and is hoisted to the top of the scope. However, if you were to write the same code with “let” or “const”, you would get an error, as these keywords are not hoisted to the top of the scope:

javascript
console.log(a); // Uncaught ReferenceError: a is not defined
let a = 10;

In conclusion, hoisting is a behavior in JavaScript that allows variables and functions declared in a code block to be accessible before they are defined or declared. While it can make your code more readable and easier to write, it’s important to understand how it works and how to use it properly to avoid unexpected behavior and bugs.

 

Exapmle:

javascript
function greet() { 
console.log(message); // Output: undefined 
var message = "Hello, world!"
console.log(message); // Output: "Hello, world!" 
greet();

In this example, greet() contains a variable declaration (var message = "Hello, world!";) and two console.log() statements. However, the first console.log() statement appears before the variable declaration.

Despite this, the code still runs without error and logs undefined to the console before logging "Hello, world!". This is because of hoisting, which is a JavaScript mechanism where variable and function declarations are moved to the top of their respective scopes before code execution.

So, in this case, even though the message variable is declared after the first console.log() statement, it is still hoisted to the top of the greet() function before the code is executed. However, its value is not set until later, which is why the first console.log() statement logs undefined.

It’s worth noting that only the declaration of message is hoisted, not its assignment. So, if we were to move the first console.log() statement after the message assignment, it would log "Hello, world!" instead of undefined.