{"id":171,"date":"2026-05-06T16:28:45","date_gmt":"2026-05-06T16:28:45","guid":{"rendered":"https:\/\/gigz.pk\/javascript\/?post_type=lesson&#038;p=171"},"modified":"2026-05-06T16:28:47","modified_gmt":"2026-05-06T16:28:47","slug":"hoisting","status":"publish","type":"lesson","link":"https:\/\/gigz.pk\/javascript\/?lesson=hoisting","title":{"rendered":"Hoisting"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Introduction<br>Hoisting is one of the most important concepts in JavaScript. It explains how variables and functions are moved to the top of their scope during the execution phase. Understanding hoisting helps developers avoid common errors and write cleaner, more predictable code.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">What is Hoisting<br>Hoisting is JavaScript&#8217;s default behavior of moving declarations to the top of the current scope before code execution. This means you can use variables and functions before they are declared in the code.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">However, only declarations are hoisted, not initializations.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Variable Hoisting<br>Variables declared with var are hoisted to the top of their function scope. But their value is undefined until the assignment line is executed.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Example<br>If you log a variable before declaring it with var, it will not throw an error but return undefined.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Variables declared with let and const are also hoisted but remain in a temporal dead zone. Accessing them before declaration causes an error.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Function Hoisting<br>Function declarations are fully hoisted. This means you can call a function before it is defined in the code.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Function expressions are not hoisted in the same way. If a function is assigned to a variable, it behaves like a variable and cannot be used before declaration.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Types of Hoisting<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Variable Hoisting<br>Applies to var, let, and const with different behaviors<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Function Hoisting<br>Applies to function declarations fully<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Class Hoisting<br>Classes are hoisted but cannot be accessed before declaration<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Temporal Dead Zone<br>This is the time between entering scope and variable declaration where let and const cannot be accessed<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Why Hoisting Matters<br>Understanding hoisting helps prevent bugs<br>Improves code readability and structure<br>Helps in debugging unexpected undefined values<br>Encourages proper variable declaration practices<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Best Practices<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Always declare variables at the top of their scope<br>Prefer let and const over var<br>Avoid using variables before declaring them<br>Use function declarations carefully and consistently<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Conclusion<br>Hoisting is a fundamental concept in JavaScript that affects how code is executed. By understanding how it works, developers can write more reliable and error free programs.<\/p>\n\n\n<div class=\"yoast-breadcrumbs\"><span><span><a href=\"https:\/\/gigz.pk\/javascript\">Home<\/a><\/span> \u00bb <span class=\"breadcrumb_last\" aria-current=\"page\">Professional JavaScript > Advanced Concepts > Hoisting<\/span><\/span><\/div>\n\n\n<div class=\"schema-faq wp-block-yoast-faq-block\"><div class=\"schema-faq-section\" id=\"faq-question-1778084896594\"><strong class=\"schema-faq-question\"><\/strong> <p class=\"schema-faq-answer\"><\/p> <\/div> <\/div>\n\n\n\n<div class=\"schema-faq wp-block-yoast-faq-block\"><div class=\"schema-faq-section\" id=\"faq-question-1778084896367\"><strong class=\"schema-faq-question\"><\/strong> <p class=\"schema-faq-answer\"><\/p> <\/div> <\/div>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n","protected":false},"menu_order":0,"template":"","class_list":["post-171","lesson","type-lesson","status-publish","hentry"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.6 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Hoisting - Learn JavaScript with GIGZ.PK<\/title>\n<meta name=\"description\" content=\"Learn JavaScript hoisting with simple examples understand variables functions and scope to write better error free code\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/gigz.pk\/javascript\/?lesson=hoisting\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Hoisting - Learn JavaScript with GIGZ.PK\" \/>\n<meta property=\"og:description\" content=\"Learn JavaScript hoisting with simple examples understand variables functions and scope to write better error free code\" \/>\n<meta property=\"og:url\" content=\"https:\/\/gigz.pk\/javascript\/?lesson=hoisting\" \/>\n<meta property=\"og:site_name\" content=\"Learn JavaScript with GIGZ.PK\" \/>\n<meta property=\"article:modified_time\" content=\"2026-05-06T16:28:47+00:00\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":[\"WebPage\",\"FAQPage\"],\"@id\":\"https:\\\/\\\/gigz.pk\\\/javascript\\\/?lesson=hoisting\",\"url\":\"https:\\\/\\\/gigz.pk\\\/javascript\\\/?lesson=hoisting\",\"name\":\"Hoisting - Learn JavaScript with GIGZ.PK\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/gigz.pk\\\/javascript\\\/#website\"},\"datePublished\":\"2026-05-06T16:28:45+00:00\",\"dateModified\":\"2026-05-06T16:28:47+00:00\",\"description\":\"Learn JavaScript hoisting with simple examples understand variables functions and scope to write better error free code\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/gigz.pk\\\/javascript\\\/?lesson=hoisting#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/gigz.pk\\\/javascript\\\/?lesson=hoisting\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/gigz.pk\\\/javascript\\\/?lesson=hoisting#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/gigz.pk\\\/javascript\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Professional JavaScript > Advanced Concepts > Hoisting\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/gigz.pk\\\/javascript\\\/#website\",\"url\":\"https:\\\/\\\/gigz.pk\\\/javascript\\\/\",\"name\":\"Learn JavaScript with GIGZ.PK\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/gigz.pk\\\/javascript\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Hoisting - Learn JavaScript with GIGZ.PK","description":"Learn JavaScript hoisting with simple examples understand variables functions and scope to write better error free code","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/gigz.pk\/javascript\/?lesson=hoisting","og_locale":"en_US","og_type":"article","og_title":"Hoisting - Learn JavaScript with GIGZ.PK","og_description":"Learn JavaScript hoisting with simple examples understand variables functions and scope to write better error free code","og_url":"https:\/\/gigz.pk\/javascript\/?lesson=hoisting","og_site_name":"Learn JavaScript with GIGZ.PK","article_modified_time":"2026-05-06T16:28:47+00:00","twitter_card":"summary_large_image","twitter_misc":{"Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":["WebPage","FAQPage"],"@id":"https:\/\/gigz.pk\/javascript\/?lesson=hoisting","url":"https:\/\/gigz.pk\/javascript\/?lesson=hoisting","name":"Hoisting - Learn JavaScript with GIGZ.PK","isPartOf":{"@id":"https:\/\/gigz.pk\/javascript\/#website"},"datePublished":"2026-05-06T16:28:45+00:00","dateModified":"2026-05-06T16:28:47+00:00","description":"Learn JavaScript hoisting with simple examples understand variables functions and scope to write better error free code","breadcrumb":{"@id":"https:\/\/gigz.pk\/javascript\/?lesson=hoisting#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/gigz.pk\/javascript\/?lesson=hoisting"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/gigz.pk\/javascript\/?lesson=hoisting#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/gigz.pk\/javascript"},{"@type":"ListItem","position":2,"name":"Professional JavaScript > Advanced Concepts > Hoisting"}]},{"@type":"WebSite","@id":"https:\/\/gigz.pk\/javascript\/#website","url":"https:\/\/gigz.pk\/javascript\/","name":"Learn JavaScript with GIGZ.PK","description":"","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/gigz.pk\/javascript\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"}]}},"_links":{"self":[{"href":"https:\/\/gigz.pk\/javascript\/index.php?rest_route=\/wp\/v2\/lesson\/171","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gigz.pk\/javascript\/index.php?rest_route=\/wp\/v2\/lesson"}],"about":[{"href":"https:\/\/gigz.pk\/javascript\/index.php?rest_route=\/wp\/v2\/types\/lesson"}],"wp:attachment":[{"href":"https:\/\/gigz.pk\/javascript\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=171"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}