Monday, 4 Aug 2025
  • My Interests
  • My Saves
  • Try Intents
Subscribe
Focus - Code Reveals
  • Home
  • HTML
    What is Symentic HTML

    What is Symentic HTML?

    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 are the async and defer attributes in the “script” tag?

    By Chief Editor

    Difference between HTML Tag and HTML Element in HTML?

    By Chief Editor

    What is a Meta Tag in HTML?

    By Chief Editor
  • JavaScript

    What is a Closure in JavaScript?

    By Chief Editor

    What is Callback Hell in JavaScript?

    By Chief Editor

    Explain Call, Apply and Bind in JavaScript.

    By Chief Editor

    What are the Rest and Spread operators in JavaScript?

    By Chief Editor

    What is memoization in JavaScript?

    By Chief Editor

    How to Reverse a String in JavaScript: Two Essential Methods

    By Chief Editor
  • Frontend Interview

    What is Position in CSS?

    By Chief Editor

    Is JavaScript a synchronous or asynchronous language?

    By Chief Editor

    What is Block level Element and Inline Level Element?

    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

    How to Reverse a String in JavaScript: Two Essential Methods

    By Chief Editor
  • Backend Interview

    What is a Higher-Order Component (HOC) in React?

    By Chief Editor

    What is Position in CSS?

    By Chief Editor

    What is the this Keyword in JavaScript?

    By Chief Editor

    How Does React Work?

    By Chief Editor

    How to Improve the Performance of React Applications

    By Chief Editor

    What is Doctype HTML in HTML?

    By Chief Editor
  • Nodejs
  • JavaScript Interview
  • React Interview
  • Frontend Interview
  • Backend Interview
  • Contact Us
  • Advertise with Us
  • Complaint
  • Cookies Policy
  • Privacy Policy
  • Donate
  • 🔥
  • ReactJS
  • JavaScript
  • JavaScript Interview
  • React Interview
  • HTML
  • Frontend Interview
  • CSS
  • Redux
  • Javascript
  • System Design
Font ResizerAa
Focus - Code RevealsFocus - Code Reveals
  • My Saves
  • My Interests
  • My Feed
  • History
  • Technology
Search
  • Homepage
  • ReactJS
  • JavaScript
  • JavaScript Interview
  • HTML
  • CSS
  • Backend Interview
Have an existing account? Sign In
Follow US
© 2022 Code Reveals Inc. All Rights Reserved.
Home Blog 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 Redux, and how does it work?

Redux is a state management library for JavaScript applications, often used with libraries like React.…

By Chief Editor

What is Virtual DOM in React?

Virtual DOM (VDOM): The Virtual DOM is a lightweight JavaScript object (a virtual representation of…

By Chief Editor

What is Block level Element and Inline Level Element?

Block-level Elements Block-level elements occupy the entire width of their parent container (by default) and…

By Chief Editor

You Might Also Like

What are the Lexical Scope in JavaScript
Frontend InterviewJavaScript

What are the Lexical Scope in JavaScript?

By Chief Editor
JavaScriptJavaScript Interview

Difference between document.createElement and document.createElementFragement in JavaScript?

By Chief Editor
JavaScriptJavaScript Interview

What is Scope in JavaScript?

By Chief Editor
JavaScriptJavaScript Interview

What are the Rest and Spread operators in JavaScript?

By Chief Editor

Focus by CodeReveals is your dedicated platform for mastering tech interviews and advancing your development skills. Whether you’re aiming to become a Frontend Developer, Backend Developer, or simply preparing for your next big interview, we’re here to guide you every step of the way.

Our mission is to help aspiring developers gain real-world knowledge, build confidence, and succeed in technical interviews. We offer structured content, curated interview questions, coding challenges, and practical guidance — all designed by experienced professionals who understand what top tech companies are looking for.

At Improve, we don’t just teach — we prepare you to think like a developer, solve like an engineer, and present like a pro.

Join us and start improving today — because your dream tech job is within reach

Most Famous
  • HTML
  • CSS
  • JavaScript
  • NodeJS
Top Categories
  • JavaScript Interview
  • React Interview
  • Frontend Interview
  • Backend Interview
Usefull Links
  • Contact Us
  • Advertise with Us
  • Complaint
  • Cookies Policy
  • Privacy Policy
  • Donate

©2025  Code Reveals Inc. All Rights Reserved.

Welcome Back!

Sign in to your account

Username or Email Address
Password

Lost your password?