Friday, 18 Jul 2025
  • My Interests
  • My Saves
  • Try Intents
Subscribe
improve-logo improve-logo
  • Home
  • HTML

    What is a Meta Tag in HTML?

    By Chief Editor

    Difference between HTML Tag and HTML Element in HTML?

    By Chief Editor

    What are the different types of HTML tags?

    By Chief Editor

    What is Doctype HTML in HTML?

    By Chief Editor

    What is Block level Element and Inline Level Element?

    By Chief Editor
    What is Symentic HTML

    What is Symentic HTML?

    By Chief Editor
  • JavaScript

    What is Scope in JavaScript?

    By Chief Editor

    What is hoisting in JavaScript with an example?

    By Chief Editor

    Is JavaScript a synchronous or asynchronous language?

    By Chief Editor

    What is the this Keyword in JavaScript?

    By Chief Editor

    What is one-way data binding in React?

    By Chief Editor

    Explain Deep Copy and Shallow Copy in JavaScript.

    By Chief Editor
  • Frontend Interview

    How to Reverse a String in JavaScript: Two Essential Methods

    By Chief Editor

    Is JavaScript a synchronous or asynchronous language?

    By Chief Editor

    Difference Between position: relative and position: absolute in CSS

    By Chief Editor

    What are the Lexical Scope in JavaScript?

    By Chief Editor

    What is Block level Element and Inline Level Element?

    By Chief Editor

    What is Position in CSS?

    By Chief Editor
  • Backend Interview

    Explain Deep Copy and Shallow Copy in JavaScript.

    By Chief Editor

    What are the drawbacks of React?

    By Chief Editor

    What is state in React, and what are its properties?

    By Chief Editor

    What is a Closure in JavaScript?

    By Chief Editor

    What is the Event Loop in JavaScript?

    By Chief Editor

    What is React Router Dom in React?

    By Chief Editor
  • Nodejs
  • Frontend Interview
  • Backend Interview
  • React Interview
  • JavaScript Interview
  • Contacts Us
  • Advertise with Us
  • Complaint
  • Privacy Policy
  • Cookie Policy
  • Donate
  • 🔥
  • ReactJS
  • JavaScript
  • JavaScript Interview
  • React Interview
  • HTML
  • Frontend Interview
  • CSS
  • Redux
  • Javascript
  • System Design
Font ResizerAa
ImproveImprove
  • My Saves
  • My Interests
  • My Feed
  • History
  • Technology
Search
  • Homepage
  • Pages
    • Home
    • Blog Index
    • Contact Us
    • Search Page
    • 404 Page
  • Features
    • Post Headers
    • Layout
  • Personalized
    • My Feed
    • My Saves
    • My Interests
    • History
  • About
  • Categories
    • Technology
  • Categories
Have an existing account? Sign In
Follow US
© 2022 Code Reveals Inc. All Rights Reserved.

Home What is hoisting in JavaScript with an example?

JavaScript

What is hoisting in JavaScript with an example?

Chief Editor
Last updated: May 6, 2025 5:18 am
Chief Editor
Share
SHARE

Hoisting is a behavior in JavaScript where variables and function declarations are moved to the top of their containing scope during the compile phase, before the code is executed.

This means that you can use variables and functions before they are declared in the code.


How Hoisting Works:

  1. Variable Declarations (var, let, const):
    • var is hoisted with an initial value of undefined.
    • let and const are hoisted but not initialized. Accessing them before declaration results in a ReferenceError due to the temporal dead zone.
  2. Function Declarations:
    • Function declarations are hoisted with their entire function definition.
    • Function expressions and arrow functions are not hoisted like function declarations.

Examples:

1. Hoisting with var:

console.log(a); // undefined
var a = 5;
console.log(a); // 5

Explanation:
The declaration var a is hoisted to the top, but its assignment (a = 5) is not. So a is undefined when accessed before assignment.

Behind the scenes (How JavaScript sees it):

var a;
console.log(a); // undefined
a = 5;
console.log(a); // 5

2. Hoisting with let and const:

console.log(b); // ReferenceError: Cannot access 'b' before initialization
let b = 10;
console.log(c); // ReferenceError: Cannot access 'c' before initialization
const c = 15;

Explanation:
The declarations are hoisted, but they remain in the temporal dead zone (TDZ) until they are initialized.


3. Hoisting with Functions:

Function Declaration:
greet(); // Hello!

function greet() {
console.log('Hello!');
}

Explanation:
The entire function declaration is hoisted, so calling greet() before its definition works.


Function Expression:
sayHello(); // TypeError: sayHello is not a function

var sayHello = function() {
console.log('Hello!');
}

Explanation:
Only the variable sayHello is hoisted (with undefined), not the function assignment.


Key Takeaways:

Declaration TypeHoisted?Initial ValueUsable Before Declaration?
varYesundefinedYes, but gives undefined
letYesUninitializedNo (Temporal Dead Zone)
constYesUninitializedNo (Temporal Dead Zone)
Function DeclarationYesFunction BodyYes
Function ExpressionVariable HoistedundefinedNo

Best Practices:

  • Prefer using let and const over var.
  • Always declare variables at the top of their scope to avoid confusion caused by hoisting.
  • Understand TDZ when using let and const.
Share This Article
Email Copy Link Print
Previous Article Explain var let and const in JavaScript with Example.
Next Article What is a Closure in JavaScript?
Leave a Comment

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Your Trusted Source for Accurate and Timely Updates!

Our commitment to accuracy, impartiality, and delivering breaking news as it happens has earned us the trust of a vast audience. Stay ahead with real-time updates on the latest events, trends.
FacebookLike
XFollow
InstagramFollow
YoutubeSubscribe
LinkedInFollow
QuoraFollow
- Advertisement -
Ad imageAd image

Popular Posts

What is a Promise in JavaScript, and what are its parameters?

A Promise in JavaScript is an object that represents the eventual completion (or failure) of…

By Chief Editor

How Does React Work?

React is a JavaScript library used for building user interfaces, particularly for single-page applications (SPAs).It…

By Chief Editor

What is Scope in JavaScript?

Scope in JavaScript refers to the context in which variables are accessible. It determines which…

By Chief Editor

You Might Also Like

JavaScript

Explain Call, Apply and Bind in JavaScript.

By Chief Editor
JavaScript

What is a Closure in JavaScript?

By Chief Editor
JavaScriptJavaScript Interview

What is Callback Hell in JavaScript?

By Chief Editor
JavaScriptJavaScript Interview

What is one-way data binding in React?

By Chief Editor
improve-logo

Code Reveals is a cutting-edge software development company dedicated to delivering high-quality, scalable, and innovative solutions for businesses of all sizes. Our team of expert developers, designers, and engineers specializes in creating custom software, web applications, mobile apps, and enterprise solutions that are tailored to meet the unique needs of our clients.

Most Famous
  • HTML
  • CSS
  • JavaScript
  • Node
Top Categories
  • Frontend Interview
  • Backend Interview
  • React Interview
  • JavaScript Interview
Usefull Links
  • Contacts Us
  • Advertise with Us
  • Complaint
  • Privacy Policy
  • Cookie Policy
  • Donate

©2025  Code Reveals Inc. All Rights Reserved.

Welcome Back!

Sign in to your account

Username or Email Address
Password

Lost your password?