Tuesday, 1 Jul 2025
  • My Feed
  • My Interests
  • My Saves
  • History
  • Blog
Subscribe
Code Reveals
  • Home
  • HTML

    What is Block level Element and Inline Level Element?

    By Chief Editor

    Difference between HTML Tag and HTML Element in HTML?

    By Chief Editor

    What is the difference between “HTML” and “HTML5”?

    By Chief Editor
    What is Symentic HTML

    What is Symentic HTML?

    By Chief Editor

    What is a Meta Tag in HTML?

    By Chief Editor

    What are the async and defer attributes in the “script” tag?

    By Chief Editor
  • JavaScript

    What is the this Keyword in JavaScript?

    By Chief Editor

    Explain var let and const in JavaScript with Example.

    By Chief Editor

    What is Scope in JavaScript?

    By Chief Editor

    What is the Event Loop in JavaScript?

    By Chief Editor

    How to Reverse a String in JavaScript: Two Essential Methods

    By Chief Editor

    Is JavaScript a synchronous or asynchronous language?

    By Chief Editor
  • Frontend Interview

    Is JavaScript a synchronous or asynchronous language?

    By Chief Editor

    What is Position in CSS?

    By Chief Editor

    Difference Between position: relative and position: absolute in CSS

    By Chief Editor

    How to Reverse a String in JavaScript: Two Essential Methods

    By Chief Editor

    Explain Deep Copy and Shallow Copy in JavaScript.

    By Chief Editor

    What is Symentic HTML?

    By Chief Editor
  • Backend Interview

    What is Redux, and how does it work?

    By Chief Editor

    What is the Event Loop in JavaScript?

    By Chief Editor

    What are the Lexical Scope in JavaScript?

    By Chief Editor

    What is middleware, and what is React Thunk?

    By Chief Editor

    Explain Deep Copy and Shallow Copy in JavaScript.

    By Chief Editor

    Difference Between position: relative and position: absolute in CSS

    By Chief Editor
  • Other
    • Contact Us
  • Frontend Interview
  • Backend Interview
  • React Interview
  • JavaScript Interview
  • Contacts Us
  • Advertise with Us
  • Complaint
  • Privacy Policy
  • Cookie Policy
  • Submit a Tip
  • 🔥
  • ReactJS
  • JavaScript
  • JavaScript Interview
  • React Interview
  • HTML
  • Frontend Interview
  • CSS
  • Redux
  • Javascript
  • System Design
Font ResizerAa
Code RevealsCode Reveals
  • 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

Difference between HTML Tag and HTML Element in HTML?

In HTML, the terms HTML tag and HTML element are often used interchangeably, but they…

By Chief Editor

What is Callback Hell in JavaScript?

Callback Hell in JavaScript refers to a situation where you have multiple nested callbacks, making…

By Chief Editor

How to Improve the Performance of React Applications

React is a powerful library for building interactive user interfaces, but performance issues can arise…

By Chief Editor

You Might Also Like

JavaScript

Explain var let and const in JavaScript with Example.

By Chief Editor
JavaScriptJavaScript Interview

What are the Rest and Spread operators in JavaScript?

By Chief Editor
JavaScriptJavaScript Interview

What is Scope in JavaScript?

By Chief Editor
What are the Lexical Scope in JavaScript
Frontend InterviewJavaScript

What are the Lexical Scope in JavaScript?

By Chief Editor

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
  • Submit a Tip

©2025  Code Reveals Inc. All Rights Reserved.

Welcome Back!

Sign in to your account

Username or Email Address
Password

Lost your password?