JavaScript Hoisting | JavaScript for Web Development - Software Development PDF Download

Introduction

When it comes to JavaScript, understanding how hoisting works is crucial for writing clean and bug-free code. Hoisting is a JavaScript behavior where variable and function declarations are moved to the top of their respective scopes during the compilation phase, before the code is executed. This can sometimes lead to unexpected results if not properly understood. In this article, we will explore the concept of hoisting in JavaScript, providing clear examples and explanations along the way.

What is Hoisting?

Hoisting is a JavaScript mechanism that allows variables and function declarations to be moved to the top of their respective scopes during the compilation phase, even if they appear to be defined later in the code. This means that you can use variables and call functions before they are declared in your code. It's important to note that hoisting only moves the declarations themselves, not the initializations or assignments. Let's dive into some examples to understand this better.

Hoisting Variables

When a variable is hoisted, its declaration is moved to the top of the scope, but the assignment or initialization remains in place. Let's see an example:

console.log(myVariable); // Output: undefined

var myVariable = 10;

console.log(myVariable); // Output: 10

In the code above, we first try to log the value of myVariable before it is declared. As a result, the output is undefined, indicating that the variable exists but doesn't have a value yet. After the declaration and assignment on the next line, we log the value of myVariable again, and this time we get the expected output of 10.
This happens because the variable declaration var myVariable is hoisted to the top, but the assignment = 10 is not. Therefore, during the first console.log, the variable exists but doesn't have a value assigned yet.

Hoisting Function Declarations

Function declarations are also hoisted in JavaScript. This means that you can call a function before it appears in the code. Let's take a look at an example:

sayHello(); // Output: Hello!


function sayHello() {

  console.log('Hello!');

}

In the code above, we call the sayHello function before it is defined. Surprisingly, it works! The output is Hello!. This is because the function declaration function sayHello() is hoisted to the top, making it available for execution anywhere within the scope.

Hoisting Function Expressions

It's important to note that hoisting works differently for function expressions compared to function declarations. Function expressions are not hoisted in the same way. Let's see an example to understand this:

sayHello(); // Throws an error


var sayHello = function() {

  console.log('Hello!');

};

In the code above, we try to call the sayHello function before its declaration. However, this time, we get an error: TypeError: sayHello is not a function. This happens because the variable sayHello is hoisted to the top, but its value is undefined until the assignment = function() { ... } is encountered. Therefore, when we try to call it before the assignment, it throws an error.

Download the notes
JavaScript Hoisting
Download as PDF
Download as PDF

Sample Problems and Solutions

To solidify your understanding of hoisting, let's solve a couple of problems:

Problem 1: What will be the output of the following code?

console.log(myVariable);

let myVariable = 20;

Solution: The code above will throw an error: ReferenceError: Cannot access 'myVariable' before initialization. This happens because let and const declarations are not hoisted in the same way as var. The variable myVariable is hoisted but is not initialized with a value until the corresponding line is reached. Therefore, trying to access it before initialization throws an error.

Problem 2: What will be the output of the following code?

console.log(typeof myFunction);

var myFunction = function() {

  console.log('Hello!');

};

Solution: The code above will output undefined because the variable myFunction is hoisted to the top, but its value is undefined until the assignment = function() { ... } is encountered. Therefore, when we try to log its type before the assignment, it returns undefined.

Take a Practice Test
Test yourself on topics from Software Development exam
Practice Now
Practice Now

Conclusion

JavaScript hoisting is a mechanism that moves variable and function declarations to the top of their respective scopes during compilation. While variable declarations using var are hoisted, function declarations are fully hoisted. On the other hand, function expressions are not hoisted in the same way, and it's important to be mindful of their placement. By understanding hoisting, you can write more robust JavaScript code and avoid potential issues.

The document JavaScript Hoisting | JavaScript for Web Development - Software Development is a part of the Software Development Course JavaScript for Web Development.
All you need of Software Development at this link: Software Development
Are you preparing for Software Development Exam? Then you should check out the best video lectures, notes, free mock test series, crash course and much more provided by EduRev. You also get your detailed analysis and report cards along with 24x7 doubt solving for you to excel in Software Development exam. So join EduRev now and revolutionise the way you learn!
Sign up for Free Download App for Free
51 videos|30 docs|12 tests

Up next

51 videos|30 docs|12 tests
Download as PDF

Up next

Explore Courses for Software Development exam
Related Searches

mock tests for examination

,

Important questions

,

Viva Questions

,

past year papers

,

ppt

,

MCQs

,

Summary

,

Extra Questions

,

JavaScript Hoisting | JavaScript for Web Development - Software Development

,

study material

,

Free

,

video lectures

,

shortcuts and tricks

,

Objective type Questions

,

Semester Notes

,

Previous Year Questions with Solutions

,

pdf

,

JavaScript Hoisting | JavaScript for Web Development - Software Development

,

Sample Paper

,

Exam

,

practice quizzes

,

JavaScript Hoisting | JavaScript for Web Development - Software Development

;