{"id":137235,"date":"2018-08-07T12:12:56","date_gmt":"2018-08-07T12:12:56","guid":{"rendered":"https:\/\/webkul.com\/blog\/?p=137235"},"modified":"2018-08-07T13:43:02","modified_gmt":"2018-08-07T13:43:02","slug":"hoisting-in-javascript","status":"publish","type":"post","link":"https:\/\/webkul.com\/blog\/hoisting-in-javascript\/","title":{"rendered":"Hoisting in JavaScript"},"content":{"rendered":"<p>Hoisting is Pre-Scope in JavaScript. Only declarations themselves are hoisted, while assignments not.<br \/>\nFor Expample-<\/p>\n<pre class=\"brush:js\">exp();\r\n\r\nfunction exp(){\r\n   console.log(a);\r\n   var a=1;\r\n}\r\n<\/pre>\n<p>In above code snippet there will be no error but the output will be undefined because JavaScript see this code as-<\/p>\n<pre class=\"brush:js\">function exp(){\r\n   var a;\r\n   console.log(a);\r\n   a=2;\r\n}\r\nexp();\r\n<\/pre>\n<p>The declaration comes first and then assignment comes. Js Engine compile JS code before it interprets.<br \/>\nCompilation phase has to find associate all declarations with their appropriate scopes.<br \/>\nLike you think var a=1; as one statement while JS thinks it as two like-<\/p>\n<pre class=\"brush:js\">var a; \/\/ compilation phase\r\na=1; \/\/ execution phase<\/pre>\n<p>Now we will take another example-<\/p>\n<p><pre class=\"brush:js\">exp();\r\nvar exp= function abc(){\r\n};<\/pre>\n<p>It will not throw the reference error but it has no value yet. So exp() is trying to invoke undefined value which is TypeError.<br \/>\nLets take one more example-<\/p>\n<pre class=\"brush:js\">exp();\r\nabc();\r\nvar exp= function abc() {};\r\n<\/pre>\n<p>It will interpret as-<\/p>\n<pre class=\"brush:js\">var exp;\r\nexp();\r\nabc();\r\n\r\nexp = function(){\r\n    var abc= --self--\r\n}\r\n\/\/ Uncaught TypeError: exp is not a function\r\n<\/pre>\n<p>\nAll declaration in a scope regardless of where they appear, are processed first before code execution. Declaration of variable and function being &#8220;moved&#8221; to top of their respective scopes which we call hoisting.<br \/>\n<strong>NOTE- <\/strong>Declarations hoisted but assignments are not hoisted.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hoisting is Pre-Scope in JavaScript. Only declarations themselves are hoisted, while assignments not. For Expample- exp(); function exp(){ console.log(a); var a=1; } In above code snippet there will be no error but the output will be undefined because JavaScript see this code as- function exp(){ var a; console.log(a); a=2; } exp(); The declaration comes first <a href=\"https:\/\/webkul.com\/blog\/hoisting-in-javascript\/\">[&#8230;]<\/a><\/p>\n","protected":false},"author":147,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[198],"tags":[],"class_list":["post-137235","post","type-post","status-publish","format-standard","hentry","category-javascript"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v24.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Hoisting in JavaScript - Webkul Blog<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/webkul.com\/blog\/hoisting-in-javascript\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Hoisting in JavaScript - Webkul Blog\" \/>\n<meta property=\"og:description\" content=\"Hoisting is Pre-Scope in JavaScript. Only declarations themselves are hoisted, while assignments not. For Expample- exp(); function exp(){ console.log(a); var a=1; } In above code snippet there will be no error but the output will be undefined because JavaScript see this code as- function exp(){ var a; console.log(a); a=2; } exp(); The declaration comes first [...]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/webkul.com\/blog\/hoisting-in-javascript\/\" \/>\n<meta property=\"og:site_name\" content=\"Webkul Blog\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/webkul\/\" \/>\n<meta property=\"article:published_time\" content=\"2018-08-07T12:12:56+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2018-08-07T13:43:02+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-og.png\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"630\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"Devyani Dubey\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@webkul\" \/>\n<meta name=\"twitter:site\" content=\"@webkul\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Devyani Dubey\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/webkul.com\/blog\/hoisting-in-javascript\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/webkul.com\/blog\/hoisting-in-javascript\/\"},\"author\":{\"name\":\"Devyani Dubey\",\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/person\/958fb32b2b18fb51eebf847fe5d177a8\"},\"headline\":\"Hoisting in JavaScript\",\"datePublished\":\"2018-08-07T12:12:56+00:00\",\"dateModified\":\"2018-08-07T13:43:02+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/webkul.com\/blog\/hoisting-in-javascript\/\"},\"wordCount\":162,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/webkul.com\/blog\/#organization\"},\"articleSection\":[\"JavaScript\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/webkul.com\/blog\/hoisting-in-javascript\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/webkul.com\/blog\/hoisting-in-javascript\/\",\"url\":\"https:\/\/webkul.com\/blog\/hoisting-in-javascript\/\",\"name\":\"Hoisting in JavaScript - Webkul Blog\",\"isPartOf\":{\"@id\":\"https:\/\/webkul.com\/blog\/#website\"},\"datePublished\":\"2018-08-07T12:12:56+00:00\",\"dateModified\":\"2018-08-07T13:43:02+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/webkul.com\/blog\/hoisting-in-javascript\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/webkul.com\/blog\/hoisting-in-javascript\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/webkul.com\/blog\/hoisting-in-javascript\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/webkul.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Hoisting in JavaScript\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/webkul.com\/blog\/#website\",\"url\":\"https:\/\/webkul.com\/blog\/\",\"name\":\"Webkul Blog\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\/\/webkul.com\/blog\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/webkul.com\/blog\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/webkul.com\/blog\/#organization\",\"name\":\"WebKul Software Private Limited\",\"url\":\"https:\/\/webkul.com\/blog\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-logo-accent-sq.png\",\"contentUrl\":\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-logo-accent-sq.png\",\"width\":380,\"height\":380,\"caption\":\"WebKul Software Private Limited\"},\"image\":{\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/webkul\/\",\"https:\/\/x.com\/webkul\",\"https:\/\/www.instagram.com\/webkul\/\",\"https:\/\/www.linkedin.com\/company\/webkul\",\"https:\/\/www.youtube.com\/user\/webkul\/\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/person\/958fb32b2b18fb51eebf847fe5d177a8\",\"name\":\"Devyani Dubey\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/7ff7684c615fcc3c18f761582191ed2ad32be68b42aa528e37d4df94b41c095f?s=96&d=https%3A%2F%2Fcdnblog.webkul.com%2Fblog%2Fwp-content%2Fuploads%2F2019%2F10%2Feva.png&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/7ff7684c615fcc3c18f761582191ed2ad32be68b42aa528e37d4df94b41c095f?s=96&d=https%3A%2F%2Fcdnblog.webkul.com%2Fblog%2Fwp-content%2Fuploads%2F2019%2F10%2Feva.png&r=g\",\"caption\":\"Devyani Dubey\"},\"sameAs\":[\"http:\/\/webkul.com\/blog\/\"],\"url\":\"https:\/\/webkul.com\/blog\/author\/devyani107\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Hoisting in JavaScript - Webkul Blog","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:\/\/webkul.com\/blog\/hoisting-in-javascript\/","og_locale":"en_US","og_type":"article","og_title":"Hoisting in JavaScript - Webkul Blog","og_description":"Hoisting is Pre-Scope in JavaScript. Only declarations themselves are hoisted, while assignments not. For Expample- exp(); function exp(){ console.log(a); var a=1; } In above code snippet there will be no error but the output will be undefined because JavaScript see this code as- function exp(){ var a; console.log(a); a=2; } exp(); The declaration comes first [...]","og_url":"https:\/\/webkul.com\/blog\/hoisting-in-javascript\/","og_site_name":"Webkul Blog","article_publisher":"https:\/\/www.facebook.com\/webkul\/","article_published_time":"2018-08-07T12:12:56+00:00","article_modified_time":"2018-08-07T13:43:02+00:00","og_image":[{"width":1200,"height":630,"url":"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-og.png","type":"image\/png"}],"author":"Devyani Dubey","twitter_card":"summary_large_image","twitter_creator":"@webkul","twitter_site":"@webkul","twitter_misc":{"Written by":"Devyani Dubey","Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/webkul.com\/blog\/hoisting-in-javascript\/#article","isPartOf":{"@id":"https:\/\/webkul.com\/blog\/hoisting-in-javascript\/"},"author":{"name":"Devyani Dubey","@id":"https:\/\/webkul.com\/blog\/#\/schema\/person\/958fb32b2b18fb51eebf847fe5d177a8"},"headline":"Hoisting in JavaScript","datePublished":"2018-08-07T12:12:56+00:00","dateModified":"2018-08-07T13:43:02+00:00","mainEntityOfPage":{"@id":"https:\/\/webkul.com\/blog\/hoisting-in-javascript\/"},"wordCount":162,"commentCount":0,"publisher":{"@id":"https:\/\/webkul.com\/blog\/#organization"},"articleSection":["JavaScript"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/webkul.com\/blog\/hoisting-in-javascript\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/webkul.com\/blog\/hoisting-in-javascript\/","url":"https:\/\/webkul.com\/blog\/hoisting-in-javascript\/","name":"Hoisting in JavaScript - Webkul Blog","isPartOf":{"@id":"https:\/\/webkul.com\/blog\/#website"},"datePublished":"2018-08-07T12:12:56+00:00","dateModified":"2018-08-07T13:43:02+00:00","breadcrumb":{"@id":"https:\/\/webkul.com\/blog\/hoisting-in-javascript\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/webkul.com\/blog\/hoisting-in-javascript\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/webkul.com\/blog\/hoisting-in-javascript\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/webkul.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Hoisting in JavaScript"}]},{"@type":"WebSite","@id":"https:\/\/webkul.com\/blog\/#website","url":"https:\/\/webkul.com\/blog\/","name":"Webkul Blog","description":"","publisher":{"@id":"https:\/\/webkul.com\/blog\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/webkul.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/webkul.com\/blog\/#organization","name":"WebKul Software Private Limited","url":"https:\/\/webkul.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/webkul.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-logo-accent-sq.png","contentUrl":"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-logo-accent-sq.png","width":380,"height":380,"caption":"WebKul Software Private Limited"},"image":{"@id":"https:\/\/webkul.com\/blog\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/webkul\/","https:\/\/x.com\/webkul","https:\/\/www.instagram.com\/webkul\/","https:\/\/www.linkedin.com\/company\/webkul","https:\/\/www.youtube.com\/user\/webkul\/"]},{"@type":"Person","@id":"https:\/\/webkul.com\/blog\/#\/schema\/person\/958fb32b2b18fb51eebf847fe5d177a8","name":"Devyani Dubey","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/webkul.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/7ff7684c615fcc3c18f761582191ed2ad32be68b42aa528e37d4df94b41c095f?s=96&d=https%3A%2F%2Fcdnblog.webkul.com%2Fblog%2Fwp-content%2Fuploads%2F2019%2F10%2Feva.png&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/7ff7684c615fcc3c18f761582191ed2ad32be68b42aa528e37d4df94b41c095f?s=96&d=https%3A%2F%2Fcdnblog.webkul.com%2Fblog%2Fwp-content%2Fuploads%2F2019%2F10%2Feva.png&r=g","caption":"Devyani Dubey"},"sameAs":["http:\/\/webkul.com\/blog\/"],"url":"https:\/\/webkul.com\/blog\/author\/devyani107\/"}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/posts\/137235","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/users\/147"}],"replies":[{"embeddable":true,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/comments?post=137235"}],"version-history":[{"count":12,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/posts\/137235\/revisions"}],"predecessor-version":[{"id":137284,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/posts\/137235\/revisions\/137284"}],"wp:attachment":[{"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/media?parent=137235"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/categories?post=137235"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/tags?post=137235"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}