{"id":99702,"date":"2026-01-28T19:05:43","date_gmt":"2026-01-28T13:35:43","guid":{"rendered":"https:\/\/www.guvi.in\/blog\/?p=99702"},"modified":"2026-02-26T12:10:01","modified_gmt":"2026-02-26T06:40:01","slug":"what-is-hoisting-in-javascript","status":"publish","type":"post","link":"https:\/\/www.guvi.in\/blog\/what-is-hoisting-in-javascript\/","title":{"rendered":"What Is Hoisting In JavaScript"},"content":{"rendered":"\n<p>Imagine reading JavaScript code from top to bottom, but the browser seems to read it in a different order. A variable is used first, declared later, and the code still runs. For beginners, this feels confusing and hard to trust.<\/p>\n\n\n\n<p>This behavior is called hoisting in JavaScript. Many beginners encounter this while learning JavaScript and are unsure why it happens. This blog is written for beginners to clearly explain what hoisting in JavaScript is, using simple examples and easy explanations.<\/p>\n\n\n\n<p><strong>Quick Answer<\/strong><\/p>\n\n\n\n<p>Hoisting in JavaScript is a behavior where the JavaScript engine moves variable and function declarations to the top of their scope before the code runs. This allows some variables and functions to be used before they are written in the code. Only declarations are hoisted, not the values assigned to them, which explains many unexpected results in JavaScript.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>What Is Hoisting In JavaScript?<\/strong><\/h2>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" width=\"1200\" height=\"630\" src=\"https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2026\/02\/What-Is-Hoisting-In-JavaScript_-1200x630.png\" alt=\"Illustration showing what is hoisting in javascript.\" class=\"wp-image-102449\" srcset=\"https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2026\/02\/What-Is-Hoisting-In-JavaScript_-1200x630.png 1200w, https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2026\/02\/What-Is-Hoisting-In-JavaScript_-300x158.png 300w, https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2026\/02\/What-Is-Hoisting-In-JavaScript_-768x403.png 768w, https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2026\/02\/What-Is-Hoisting-In-JavaScript_-1536x806.png 1536w, https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2026\/02\/What-Is-Hoisting-In-JavaScript_-2048x1075.png 2048w, https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2026\/02\/What-Is-Hoisting-In-JavaScript_-150x79.png 150w\" sizes=\"(max-width: 1200px) 100vw, 1200px\" title=\"\"><\/figure>\n\n\n\n<p>In JavaScript, sometimes variables or functions can be used before they are written in the code, and the program still works. This happens because of a behavior called hoisting. Hoisting in JavaScript moves all variable and function declarations to the top of their scope during memory allocation, even before the code runs.<\/p>\n\n\n\n<p>This does not mean the values assigned to variables are moved. Only the declarations are prepared first. Understanding hoisting in JavaScript helps beginners see how JavaScript reads their code, why some variables or functions exist before they appear, and how to avoid unexpected errors.<\/p>\n\n\n\n<p><strong>Key Points:<\/strong><\/p>\n\n\n\n<p>\u2022 <strong>Variables Are Hoisted &#8211; <\/strong>&nbsp;Variables declared with var are hoisted and initialized with undefined.<br>\u2022 <strong>Functions Are Hoisted &#8211;<\/strong> Function declarations are fully hoisted, allowing them to be called before their definition.<br>\u2022 <strong>Only Declarations Are Hoisted &#8211;<\/strong> Assigned values are not hoisted, only the declaration itself.<br>\u2022 <strong>No Physical Movement &#8211; <\/strong>JavaScript does not physically move code lines. It prepares memory first.<br>\u2022 <strong>Scope Matters &#8211; <\/strong>&nbsp;Hoisting occurs within the variable or function\u2019s scope, whether global or local.<\/p>\n\n\n\n<p>Do check out <strong>HCL GUVI\u2019s <\/strong><a href=\"https:\/\/www.guvi.in\/courses\/programming\/javascript-for-beginners\/?utm_source=blog&amp;utm_medium=hyperlink&amp;utm_campaign=What-Is-Hoisting-In-JavaScript\" target=\"_blank\" rel=\"noreferrer noopener\"><strong>JavaScript for Beginners course<\/strong><\/a> to learn more about how hoisting, variables, functions, and other core JavaScript concepts work in practice. This course offers hands-on examples and exercises to help you write clean and error-free JavaScript code with confidence.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Hoisting Examples In JavaScript<\/strong><\/h2>\n\n\n\n<p>Hoisting in <a href=\"https:\/\/www.guvi.in\/hub\/javascript\/what-is-javascript\/\" target=\"_blank\" rel=\"noreferrer noopener\">JavaScript<\/a> behaves differently depending on whether you are working with variables or functions. This section will give you clear examples to understand how hoisting works in real code. You will see how variables declared with var, let, and const behave, how function declarations are hoisted, and how function expressions differ.<\/p>\n\n\n\n<p>In this section, we will cover:<\/p>\n\n\n\n<ol>\n<li>Hoisting With var<\/li>\n\n\n\n<li>Hoisting With let And const<\/li>\n\n\n\n<li>Function Hoisting<\/li>\n\n\n\n<li>Hoisting With Function Expressions<\/li>\n<\/ol>\n\n\n\n<p>These examples will make it easier to predict how your JavaScript code will run and avoid common mistakes caused by hoisting.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>1. Hoisting With var<\/strong><\/h3>\n\n\n\n<p><a href=\"https:\/\/www.guvi.in\/hub\/javascript\/variable-in-javascript\/\" target=\"_blank\" rel=\"noreferrer noopener\">Variables<\/a> declared with var are hoisted to the top of their function or global scope. This means the variable name is known to JavaScript before the code runs, but it is initialized with undefined. Accessing the variable before the assignment line does not throw an error, but its value will be undefined. This behavior often leads to confusion for beginners who expect variables to exist only after their declaration.<\/p>\n\n\n\n<p><strong>Key Points:<\/strong><\/p>\n\n\n\n<p>\u2022 <strong>Hoisted With undefined &#8211;<\/strong> Variables declared with var are hoisted and initialized with undefined.<br>\u2022 <strong>Function Scope &#8211;<\/strong> var is function-scoped, not block-scoped, so hoisting happens within the function.<br>\u2022 <strong>Accessible Before Declaration &#8211;<\/strong> You can reference the variable before it is written, but it will return undefined.<\/p>\n\n\n\n<p><strong>Code Example:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>console.log(name); \/\/ Output: undefined\nvar name = \"Alice\";\nconsole.log(name); \/\/ Output: Alice\n<\/code><\/pre>\n\n\n\n<p><strong>Explanation:<\/strong><strong><br><\/strong>In this code, the variable name is hoisted to the top of the scope and initialized with undefined. That is why the first console.log prints undefined. After the assignment, it prints &#8220;Alice&#8221;.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>2. Hoisting With let And const<\/strong><\/h3>\n\n\n\n<p>Variables declared with let and const are hoisted, but unlike var, they are not initialized. These variables remain in the temporal dead zone from the start of the block until their declaration is reached. Trying to access them before the declaration throws a ReferenceError. This makes let and const safer to use, as they prevent accidental use of variables before initialization.<\/p>\n\n\n\n<p><strong>Key Points:<\/strong><\/p>\n\n\n\n<p>\u2022 <strong>Hoisted But Not Initialized &#8211;<\/strong> let and const variables are hoisted but remain uninitialized until their declaration line.<br>\u2022 <strong>Temporal Dead Zone &#8211;<\/strong> Accessing them before declaration throws a ReferenceError.<br>\u2022 <strong>Block Scope &#8211;<\/strong> Both let and const are block-scoped, so hoisting only occurs within the block.<\/p>\n\n\n\n<p><strong>Code Example:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>console.log(age); \/\/ ReferenceError\nlet age = 25;\nconsole.log(city); \/\/ ReferenceError\nconst city = \"London\";\n<\/code><\/pre>\n\n\n\n<p><strong>Explanation:<\/strong><strong><br><\/strong>Here, age and city are hoisted but uninitialized. Attempting to access them before declaration results in a ReferenceError. This demonstrates the temporal dead zone, which prevents using the variable before it is defined.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>3. Function Hoisting<\/strong><\/h3>\n\n\n\n<p><a href=\"https:\/\/www.guvi.in\/blog\/guide-for-functions-in-javascript\/\" target=\"_blank\" rel=\"noreferrer noopener\">Function<\/a> declarations in JavaScript are fully hoisted. This means both the function name and its body are moved to the top of their scope during memory allocation. Because of this, you can call a function anywhere within its scope, even before the line where it is defined. This behavior is useful for organizing code and creating helper functions that are used before they are defined in the source code.<\/p>\n\n\n\n<p><strong>Key Points:<\/strong><\/p>\n\n\n\n<p>\u2022 <strong>Fully Hoisted &#8211;<\/strong> Function declarations are completely hoisted to the top of their scope.<br>\u2022 <strong>Callable Before Declaration &#8211;<\/strong> You can call the function anywhere in the scope.<br>\u2022 <strong>Function Scope &#8211;<\/strong> Hoisting respects the function\u2019s scope, whether global or local.<\/p>\n\n\n\n<p><strong>Code Example:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>greet(); \/\/ Output: Hello, World!\nfunction greet() {\n  console.log(\"Hello, World!\");\n}\n<\/code><\/pre>\n\n\n\n<p><strong>Explanation:<\/strong><strong><br><\/strong>The function greet is fully hoisted, so it can be called before its definition in the code. This is different from variables, which may only be initialized with undefined.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>4. Hoisting With Function Expressions<\/strong><\/h3>\n\n\n\n<p>Function expressions, including arrow functions, behave differently from function declarations. Only the variable they are assigned to is hoisted, not the function itself. Using a function expression before it is assigned depends on whether the variable is declared with var, let, or const. Accessing it too early can lead to errors or undefined values.<\/p>\n\n\n\n<p><strong>Key Points:<\/strong><\/p>\n\n\n\n<p>\u2022 <strong>var Function Expression &#8211;<\/strong> Hoisted as undefined; calling it before assignment will result in an error if invoked.<br>\u2022 <strong>let\/const Function Expression &#8211;<\/strong> Hoisted but uninitialized; calling before assignment throws ReferenceError.<br>\u2022 <strong>Assignment Matters &#8211;<\/strong> Only after the variable is assigned does it hold the function.<\/p>\n\n\n\n<p><strong>Code Example:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>console.log(sayHi); \/\/ Output: undefined\nvar sayHi = function() {\n  console.log(\"Hi!\");\n};\nconsole.log(hello); \/\/ ReferenceError\nlet hello = () =&gt; console.log(\"Hello!\");\n<\/code><\/pre>\n\n\n\n<p><strong>Explanation:<\/strong><strong><br><\/strong>The var function expression sayHi is hoisted as undefined, so referencing it before assignment does not throw ReferenceError, but calling it would. The let arrow function hello is in the temporal dead zone, so accessing it before declaration throws a ReferenceError.<\/p>\n\n\n\n<p>Do check out HCL GUVI\u2019s<strong> <\/strong><a href=\"https:\/\/www.guvi.in\/ide\/?utm_source=blog&amp;utm_medium=hyperlink&amp;utm_campaign=What-Is-Hoisting-In-JavaScript\" target=\"_blank\" rel=\"noreferrer noopener\"><strong>Online IDE<\/strong><\/a> to practice JavaScript concepts like hoisting by writing and running code instantly in your browser. It helps you experiment with variable declarations, function behavior, and execution flow in real time, making it easier to understand how JavaScript actually works.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Common Errors And Best Practices<\/strong><\/h2>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" width=\"1200\" height=\"630\" src=\"https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2026\/02\/Common-Errors-And-Best-Practices@2x-1200x630.png\" alt=\"Infographic showing the common errors and best practices of hoisting in javascript.\" class=\"wp-image-102450\" srcset=\"https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2026\/02\/Common-Errors-And-Best-Practices@2x-1200x630.png 1200w, https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2026\/02\/Common-Errors-And-Best-Practices@2x-300x158.png 300w, https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2026\/02\/Common-Errors-And-Best-Practices@2x-768x403.png 768w, https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2026\/02\/Common-Errors-And-Best-Practices@2x-1536x806.png 1536w, https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2026\/02\/Common-Errors-And-Best-Practices@2x-2048x1075.png 2048w, https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2026\/02\/Common-Errors-And-Best-Practices@2x-150x79.png 150w\" sizes=\"(max-width: 1200px) 100vw, 1200px\" title=\"\"><\/figure>\n\n\n\n<p>When learning hoisting in JavaScript, beginners often face unexpected behavior that can feel confusing. Most of these issues are not caused by complex problems, but by small mistakes related to variable declarations, function usage, or scope. Understanding these common errors and following best practices can help avoid bugs and make your code more predictable.<\/p>\n\n\n\n<p><strong>Common Errors:<\/strong><\/p>\n\n\n\n<p>\u2022 <strong>Using var Before Assignment<\/strong> Accessing a var variable before assigning a value returns undefined.<br>\u2022 <strong>Accessing let or const Too Early<\/strong> Trying to use let or const variables before declaration causes a ReferenceError.<br>\u2022 <strong>Calling Function Expressions Too Early<\/strong> Function expressions cannot be called before assignment, unlike function declarations.<br>\u2022 <strong>Confusing Function Declarations and Expressions<\/strong> Expecting function expressions to behave like declarations often causes errors.<br>\u2022 <strong>Misunderstanding Scope<\/strong> Variables or functions may be hoisted differently depending on whether they are in global, function, or block scope.<\/p>\n\n\n\n<p><strong>Best Practices:<\/strong><\/p>\n\n\n\n<p>\u2022 <strong>Declare Variables at the Top<\/strong> Always declare variables at the start of their scope to avoid confusion.<br>\u2022 <strong>Use let and const Carefully<\/strong> Avoid accessing let and const variables before declaration to prevent ReferenceErrors.<br>\u2022 <strong>Call Functions After Assignment<\/strong> Only call function expressions after they have been assigned.<br>\u2022 <strong>Prefer Function Declarations for Early Calls<\/strong> Use function declarations if you need to call functions before their code appears.<br>\u2022 <strong>Understand Scope<\/strong> Be aware of whether your variable or function is global, function-scoped, or block-scoped to avoid hoisting surprises.<\/p>\n\n\n\n<p>Do check out HCL GUVI\u2019s<strong> <\/strong><a href=\"https:\/\/www.guvi.in\/hub\/javascript\/?utm_source=blog&amp;utm_medium=hyperlink&amp;utm_campaign=What-Is-Hoisting-In-JavaScript\" target=\"_blank\" rel=\"noreferrer noopener\"><strong>JavaScript Hub<\/strong><\/a> to deepen your understanding of core JavaScript concepts like hoisting, variables, functions, and execution flow. This hub brings together structured articles, explanations, and learning resources that help beginners connect theory with real coding behavior.<\/p>\n\n\n\n<div style=\"background-color: #099f4e; border: 3px solid #110053; border-radius: 12px; padding: 18px 22px; color: #FFFFFF; font-size: 18px; font-family: Montserrat, Helvetica, sans-serif; line-height: 1.6; box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15); max-width: 750px; margin: 22px auto;\">\n  <h3 style=\"margin-top: 0; font-size: 22px; font-weight: 700; color: #ffffff;\">\ud83d\udca1 Did You Know?<\/h3>\n  <ul style=\"padding-left: 20px; margin: 10px 0;\">\n    <li>You can call a function declaration before it appears in the code, but calling a function expression before assignment will fail.<\/li>\n    <li>Variables declared with var are hoisted and initialized with undefined, which can lead to unexpected results if used too early.<\/li>\n    <li>Hoisting does not physically move code; JavaScript only sets up declarations in memory first, which often surprises beginners.<\/li>\n  <\/ul>\n<\/div>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Conclusion<\/strong><\/h2>\n\n\n\n<p>Understanding hoisting in JavaScript is essential for writing predictable and error-free code. By knowing how variables and functions are hoisted, you can avoid common mistakes and make better decisions about where and how to declare them.<\/p>\n\n\n\n<p>Remember that var variables are initialized with undefined, let and const exist in the temporal dead zone, and function declarations are fully hoisted while function expressions are not. Keeping these rules in mind will help you write cleaner, more reliable JavaScript code.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>FAQs<\/strong><\/h2>\n\n\n<div id=\"rank-math-faq\" class=\"rank-math-block\">\n<div class=\"rank-math-list \">\n<div id=\"faq-question-1769582634410\" class=\"rank-math-list-item\">\n<h3 class=\"rank-math-question \"><strong>1. Can hoisting cause performance issues in large JavaScript applications?<\/strong><\/h3>\n<div class=\"rank-math-answer \">\n\n<p>Hoisting itself does not significantly affect performance, but relying on it heavily can make code harder to read and maintain, which may indirectly impact efficiency in large projects.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1769582663456\" class=\"rank-math-list-item\">\n<h3 class=\"rank-math-question \"><strong>2. Does hoisting behave differently inside strict mode?<\/strong><\/h3>\n<div class=\"rank-math-answer \">\n\n<p>No, hoisting works the same in strict mode, but strict mode helps prevent common mistakes like using undeclared variables, reducing errors caused by hoisting.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1769582683963\" class=\"rank-math-list-item\">\n<h3 class=\"rank-math-question \"><strong>3. How does hoisting affect JavaScript modules or ES6 imports?<\/strong><\/h3>\n<div class=\"rank-math-answer \">\n\n<p>In ES6 modules, variables and functions are still hoisted within the module scope, but imports are handled differently and are not hoisted like regular variables or functions.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1769582710609\" class=\"rank-math-list-item\">\n<h3 class=\"rank-math-question \"><strong>4. Can global variables affect hoisting behavior in functions?<\/strong><\/h3>\n<div class=\"rank-math-answer \">\n\n<p>Yes, if a variable is declared globally, it may overshadow local variables, and understanding hoisting ensures you don\u2019t accidentally access or overwrite global variables unintentionally.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1769582731336\" class=\"rank-math-list-item\">\n<h3 class=\"rank-math-question \"><strong>5. Are there debugging tools to visualize hoisting in JavaScript?<\/strong><\/h3>\n<div class=\"rank-math-answer \">\n\n<p>Yes, browser developer tools like Chrome DevTools allow you to step through code execution, inspect scopes, and see how variables and functions are hoisted during runtime.<\/p>\n\n<\/div>\n<\/div>\n<\/div>\n<\/div>","protected":false},"excerpt":{"rendered":"<p>Imagine reading JavaScript code from top to bottom, but the browser seems to read it in a different order. A variable is used first, declared later, and the code still runs. For beginners, this feels confusing and hard to trust. This behavior is called hoisting in JavaScript. Many beginners encounter this while learning JavaScript and [&hellip;]<\/p>\n","protected":false},"author":65,"featured_media":102447,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[429,907],"tags":[],"views":"527","authorinfo":{"name":"Jebasta","url":"https:\/\/www.guvi.in\/blog\/author\/jebasta\/"},"thumbnailURL":"https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2026\/01\/What-Is-Hoisting-In-JavaScript-1-300x116.png","jetpack_featured_media_url":"https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2026\/01\/What-Is-Hoisting-In-JavaScript-1.png","_links":{"self":[{"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/posts\/99702"}],"collection":[{"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/users\/65"}],"replies":[{"embeddable":true,"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/comments?post=99702"}],"version-history":[{"count":5,"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/posts\/99702\/revisions"}],"predecessor-version":[{"id":102451,"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/posts\/99702\/revisions\/102451"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/media\/102447"}],"wp:attachment":[{"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/media?parent=99702"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/categories?post=99702"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/tags?post=99702"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}