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

    Difference between HTML Tag and HTML Element in HTML?

    By Chief Editor
    What is Symentic HTML

    What is Symentic 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 the difference between “HTML” and “HTML5”?

    By Chief Editor

    What is Block level Element and Inline Level Element?

    By Chief Editor
  • JavaScript

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

    By Chief Editor

    What is Arrow and Normal Function in JavaScript?

    By Chief Editor

    Explain Call, Apply and Bind in JavaScript.

    By Chief Editor

    What is the Event Loop in JavaScript?

    By Chief Editor

    Is JavaScript a synchronous or asynchronous language?

    By Chief Editor

    What is memoization in JavaScript?

    By Chief Editor
  • Frontend Interview

    Is JavaScript a synchronous or asynchronous language?

    By Chief Editor

    What is Symentic HTML?

    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 Block level Element and Inline Level Element?

    By Chief Editor

    What are the Lexical Scope in JavaScript?

    By Chief Editor
  • Backend Interview

    How Can You Share Data Between Components in React?

    By Chief Editor

    What is Doctype HTML in HTML?

    By Chief Editor

    Lazy Loading in React.js: Boosting Performance and Reducing Load Time

    By Chief Editor

    How to Improve the Performance of React Applications

    By Chief Editor

    What is Life Cycle method in React?

    By Chief Editor

    What are the different types of HTML tags?

    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

How Does React Work?

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

By Chief Editor

How to Reverse a String in JavaScript: Two Essential Methods

Reversing a string is one of the most common beginner-level tasks in programming. It’s a…

By Chief Editor

What is props drilling in React?

Props Drilling is a situation in React where data (props) needs to be passed through…

By Chief Editor

You Might Also Like

JavaScriptJavaScript Interview

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

By Chief Editor
JavaScriptJavaScript Interview

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

By Chief Editor
JavaScriptJavaScript Interview

What is the this Keyword in JavaScript?

By Chief Editor
Frontend InterviewJavaScript

Explain Deep Copy and Shallow Copy 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?