Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

Sorry, you do not have permission to ask a question, You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please type your username.

Please type your E-Mail.

Please choose an appropriate title for the post.

Please choose the appropriate section so your post can be easily searched.

Please choose suitable Keywords Ex: post, video.

Browse

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Logo Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Logo

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Navigation

  • Home
  • About Us
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • About Us
  • Contact Us
Home/ Questions/Q 1755

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Latest Questions

Author
  • 62k
Author
Asked: November 25, 20242024-11-25T11:05:10+00:00 2024-11-25T11:05:10+00:00

Hoisting In JavaScript

  • 62k

This blog is originally published on my Blog website, where you can find the full version with detailed insights and examples. Click the link below to read the complete article and explore more tech-related content!
👉 Click Here

Hello DEV community I gonna discuss the Hoisting in JavaScript in every aspect such as how Hoisting works with functions, variables (var, let, and const), and classes.

Table Of Contents

  1. Introduction
  2. Hoisting Function Scope Variable: var
  3. Hoisting Scope Variable: let
  4. Hoisting Constant: const
  5. Hoisting Functions
  6. Hoisting Classes
  7. Conclusion

Introduction

Hoisting is the default behavior of moving all the declarations at the top of the scope before code execution.
It gives us an advantage that:

  • No matter where functions and variables are declared, they are moved to the top of their scope regardless of whether their scope is global or local.
  • It allows us to call functions before even writing them in our code.

Variable Life Cycle: Declaration –> Initialization/Assignment –> Usage

Variable lifecycle

JavaScript allows us to both declare and initialize our variables simultaneously, this is the most used pattern:

example2

Note: Always remember that in the background the JavaScript is first declaring the variable and then initializing them.

in JavaScript, undeclared variables do not exist until the code assigning them is executed. Therefore, assigning a value to an undeclared variable implicitly creates it as a global variable when the assignment is executed. This means that all undeclared variables are global variables.

hoisting example2

Function Life Cycle: Declaration –> Usage
In the case of Function, a function can be declared and later used (or invoked) in the application. The initialization is omitted. For instance:

Function Life Cycle

Hoisting Function Scope Variable: var

Hoisting with var is somewhat different as when compared to let/const. Let’s make use of var and see how hoisting works:

global scope

The above code interpreter sees differently:

global scope 2

Hoisting with var work same in function scoped variable as it shows above.

Hoisting Block Scope Variable: var

Tell now all we discussed is about general hoisting which is 100% applicable on functions and var type variables as defined because let and const types variables cannot access/use able before declaration.

But hoisting work mechanism is different for let types variables.
As if we try to access the let variable before declaring it causes a ReferenceError that the is not defined.

Hoisting Block Scope Variable 1

By default a declared yet not initialized variable has an undefined value.

Hoisting Block Scope Variable ex.2

Hoisting Constant: const

The constant statement creates and initializes constants inside the block scope:

Hoisting Constant ex.1

When a constant is defined, it must be initialized with a value in the same const statement if this rule does not follow the error SyntaxError: Missing initializer in const declaration will be thrown.
After declaration and initialization, the value of a constant cannot be modified:

Hoisting Constant ex.2

The constants cannot be accessed before declaration because of the temporal dead zone. When accessed before declaration, JavaScript throws an error: ReferenceError: is not defined.

Hoisting Constant ex.3

const hoisting has the same behavior as the variables declared with the let statement.

Hoisting Constant ex.4

Hoisting Functions

Hoisting in a function declaration allows using of the function anywhere in the enclosing scope, even before the declaration.
The following code from the start invokes a function, and defines it:

Hoisting Functions ex.1

The code works nicely because equal() is created by a function declaration and hoisted to the top of the scope.

Note: Function declaration function declaration function <name>() {...} and function expression var <name> = function() {...} both are used to create functions, however have different hoisting mechanisms.

The following sample demonstrates the distinction between both ways:

Hoisting Functions ex.2

The sum is hoisted entirely and can be called before the declaration.
However subtract is declared using a variable statement and is hoisted too, but has an undefined value when invoked. This scenario throws an error based on the type of variable to which a function is assigned

  • If the variable is var then the error is: `TypeError: subtraction is not a function.

  • If the variable is let or const then the error is: ReferenceError: Cannot access 'subtract' before initialization

Hoisting Classes

The class variables are registered at the beginning of the block scope. But if you try to access the class before the definition, JavaScript throws ReferenceError: Cannot access '<class_name>' before initialization.

So the correct approach is to first declare the class and later use it to instantiate objects.

Hoisting in class declarations is similar to variables declared with a let statement

Let's see what happens if a class is instantiated before declaration:

Hoisting Class Example-1

The same goes with the class creation with variable declaration statement:

Hoisting Class Example-1

Conclusion

The summary of the post is

  • Hoisting allow us to use variables, but it depends on the type of variable you are using.

  • Hoisting allow us to use functions before the declaration, but it depends on in which way you are declaring the function either the simple declaration or with a variable.

  • Hoisting the var variable allows us to access/use the variable before declaration but the value will be undefined before initialization.

  • Hoisting the let variable caused a RefrenceError if you access/use the let variable before a declaration.

  • The declared but not initialized let variable by default has an undefined value

  • const hoisting has the same behavior as the variables declared with the let statement.

  • The const variable must be initialized at the time of declaration.

  • Classes must be defined before accessing it otherwise the program will through an error ReferenceError: Cannot access '<class_name>' before initialization

Exploring new concepts in #JavaScript and sharing with others is my passion and it gives me pleasure to help and inspire people. If you have any suggestion or want to add something please comment.

If you liked the post follow me on: Twitter, Linkedin and GitHub!

discusshoistingjavascriptwebdev
  • 0 0 Answers
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

Sidebar

Ask A Question

Stats

  • Questions 4k
  • Answers 0
  • Best Answers 0
  • Users 2k
  • Popular
  • Answers
  • Author

    How to ensure that all the routes on my Symfony ...

    • 0 Answers
  • Author

    Insights into Forms in Flask

    • 0 Answers
  • Author

    Kick Start Your Next Project With Holo Theme

    • 0 Answers

Top Members

Samantha Carter

Samantha Carter

  • 0 Questions
  • 20 Points
Begginer
Ella Lewis

Ella Lewis

  • 0 Questions
  • 20 Points
Begginer
Isaac Anderson

Isaac Anderson

  • 0 Questions
  • 20 Points
Begginer

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help

Footer

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise

Querify Question Shop: Explore, ask, and connect. Join our vibrant Q&A community today!

About Us

  • About Us
  • Contact Us
  • All Users

Legal Stuff

  • Terms of Use
  • Privacy Policy
  • Cookie Policy

Help

  • Knowledge Base
  • Support

Follow

© 2022 Querify Question. All Rights Reserved

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.