{"id":15722,"date":"2014-10-12T18:43:14","date_gmt":"2014-10-12T13:13:14","guid":{"rendered":"https:\/\/tothenewco.pro\/blog\/?p=15722"},"modified":"2014-10-17T14:11:56","modified_gmt":"2014-10-17T08:41:56","slug":"variable-hoisting-in-javascript","status":"publish","type":"post","link":"https:\/\/tothenewco.pro\/blog\/variable-hoisting-in-javascript\/","title":{"rendered":"Variable Hoisting In JavaScript"},"content":{"rendered":"<p>Javascript handles function and variable declartions quite differently and you must have got stuck in one of the awkard scenarios due to it.<\/p>\n<p>First of all, you should be familiar with Scoping in JavaScript. If you are, then you would know already that <strong>JavaScript only has function scopes and no block scopes<\/strong>.<\/p>\n<p>[js]<br \/>\nvar a = &#8216;hello&#8217;;<br \/>\nconsole.log(a);<br \/>\nif(a)<br \/>\n{    console.log(a);<br \/>\n    a = &#8216;javascript&#8217;;<br \/>\n }<br \/>\nconsole.log(a);<br \/>\n[\/js]<\/p>\n<p>The result is:<br \/>\n<code><br \/>\nhello<br \/>\nhello<br \/>\njavascript<br \/>\n<\/code><\/p>\n<p>As we can see in the above example, the <strong>if<\/strong> block doesn&#8217;t create a new scope.<\/p>\n<p>Lets go into the concept of <strong>Hoisting<\/strong> now. Hoisting is a way in which JavaScript deals with variable and function declarations. What actually the JavaScript interpreter does is, it pushes all your Function and Variable declarations to the top of the containing scope.<\/p>\n<p>Lets understand this with a simple example:<\/p>\n<p>[js]<br \/>\nfunction sum() {<br \/>\n    calculate();<br \/>\n    var a = 10;<br \/>\n    var b = 20;<br \/>\n}<br \/>\n[\/js]<\/p>\n<p>\/\/Now this function will be interpreted as below<\/p>\n<p>[js]<br \/>\nfunction sum() {<br \/>\n    var a, b;<br \/>\n    calculate();<br \/>\n    a = 10;<br \/>\n    b = 20;<br \/>\n}<br \/>\n[\/js]<\/p>\n<p>Lets try some code here:<\/p>\n<p>[js]<br \/>\nfunction calculate() {<br \/>\n    sum();<br \/>\n    multiply();<br \/>\n    var a = 10;<br \/>\n    function sum(){<br \/>\n        console.log(a + b);<br \/>\n    }<br \/>\n    var b = 20;<br \/>\n    var multiply = function() {<br \/>\n        console.log(a * b);<br \/>\n    }<br \/>\n}<br \/>\ncalculate();<br \/>\n[\/js]<\/p>\n<p>The above code will throw an error : <strong>undefined is not a function<\/strong>. Lets see how this function is interpreted.<\/p>\n<p>[js]<br \/>\nfunction calculate() {<br \/>\n    var a, b, multiply;<br \/>\n    function sum(){<br \/>\n        console.log(a + b);<br \/>\n    }<br \/>\n    sum();<br \/>\n    multiply();<br \/>\n    a = 10;<br \/>\n    b = 20;<br \/>\n    multiply = function() {<br \/>\n        console.log(a * b)<br \/>\n    }<br \/>\n}<br \/>\n[\/js]<\/p>\n<p>As we can see, only the left handside of the declarations is hoisted, but that is not the case with function declarations written as <strong>sum<\/strong> method.<\/p>\n<p>The other way of declaring functions is as method <strong>multiply<\/strong>, in this case though the interpreter only takes the left hand side name. This results in an error undefined is not a function as multiply is undefiend at the time of function call.<\/p>\n<p>This is all about JavaScript Hoisting in brief.<br \/>\nHope it helps you, hoisting your own flag in JavaScript.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hoisting is a way in which JavaScript deals with variable and function declarations. What actually the JavaScript interpreter does is, it pushes all your Function and Variable declarations to the top of the containing scope.<\/p>\n","protected":false},"author":65,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":1,"footnotes":""},"categories":[1],"tags":[1528,1527,55,1529,1530],"class_list":["post-15722","post","type-post","status-publish","format-standard","hentry","category-technology","tag-functions","tag-hoisting","tag-javascript","tag-scopes","tag-variables"],"aioseo_notices":[],"aioseo_head":"\n\t\t<!-- All in One SEO 5.0.0.1 - aioseo.com -->\n\t<meta name=\"description\" content=\"Hoisting is a way in which JavaScript deals with variable and function declarations. What actually the JavaScript interpreter does is, it pushes all your Function and Variable declarations to the top of the containing scope.\" \/>\n\t<meta name=\"robots\" content=\"max-image-preview:large\" \/>\n\t<meta name=\"author\" content=\"Sakshi Tyagi\"\/>\n\t<link rel=\"canonical\" href=\"https:\/\/tothenewco.pro\/blog\/variable-hoisting-in-javascript\/\" \/>\n\t<meta name=\"generator\" content=\"All in One SEO (AIOSEO) 5.0.0.1\" \/>\n\t\t<meta property=\"og:locale\" content=\"en_US\" \/>\n\t\t<meta property=\"og:site_name\" content=\"TO THE NEW BLOG\" \/>\n\t\t<meta property=\"og:type\" content=\"blog\" \/>\n\t\t<meta property=\"og:title\" content=\"Variable Hoisting In JavaScript | TO THE NEW Blog\" \/>\n\t\t<meta property=\"og:description\" content=\"Hoisting is a way in which JavaScript deals with variable and function declarations. What actually the JavaScript interpreter does is, it pushes all your Function and Variable declarations to the top of the containing scope.\" \/>\n\t\t<meta property=\"og:url\" content=\"https:\/\/tothenewco.pro\/blog\/variable-hoisting-in-javascript\/\" \/>\n\t\t<meta property=\"og:image\" content=\"https:\/\/tothenewco.pro\/blog\/wp-content\/themes\/ttn\/images\/social-logo.png\" \/>\n\t\t<meta property=\"og:image:secure_url\" content=\"https:\/\/tothenewco.pro\/blog\/wp-content\/themes\/ttn\/images\/social-logo.png\" \/>\n\t\t<meta name=\"twitter:card\" content=\"summary\" \/>\n\t\t<meta name=\"twitter:site\" content=\"@tothenew\" \/>\n\t\t<meta name=\"twitter:title\" content=\"Variable Hoisting In JavaScript | TO THE NEW Blog\" \/>\n\t\t<meta name=\"twitter:description\" content=\"Hoisting is a way in which JavaScript deals with variable and function declarations. What actually the JavaScript interpreter does is, it pushes all your Function and Variable declarations to the top of the containing scope.\" \/>\n\t\t<meta name=\"twitter:image\" content=\"https:\/\/tothenewco.pro\/blog\/wp-content\/themes\/ttn\/images\/social-logo.png\" \/>\n\t\t<script type=\"application\/ld+json\" class=\"aioseo-schema\">\n\t\t\t{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/variable-hoisting-in-javascript\\\/#article\",\"name\":\"Variable Hoisting In JavaScript | TO THE NEW Blog\",\"headline\":\"Variable Hoisting In JavaScript\",\"author\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/author\\\/sakshi\\\/#author\"},\"publisher\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/#organization\"},\"datePublished\":\"2014-10-12T18:43:14+05:30\",\"dateModified\":\"2014-10-17T14:11:56+05:30\",\"inLanguage\":\"en-US\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/variable-hoisting-in-javascript\\\/#webpage\"},\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/variable-hoisting-in-javascript\\\/#webpage\"},\"articleSection\":\"Technology, functions, hoisting, javascript, scopes, variables\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/variable-hoisting-in-javascript\\\/#breadcrumblist\",\"itemListElement\":[{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog#listItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.tothenew.com\\\/blog\",\"nextItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/category\\\/technology\\\/#listItem\",\"name\":\"Technology\"}},{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/category\\\/technology\\\/#listItem\",\"position\":2,\"name\":\"Technology\",\"item\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/category\\\/technology\\\/\",\"nextItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/variable-hoisting-in-javascript\\\/#listItem\",\"name\":\"Variable Hoisting In JavaScript\"},\"previousItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog#listItem\",\"name\":\"Home\"}},{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/variable-hoisting-in-javascript\\\/#listItem\",\"position\":3,\"name\":\"Variable Hoisting In JavaScript\",\"previousItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/category\\\/technology\\\/#listItem\",\"name\":\"Technology\"}}]},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/#organization\",\"name\":\"TO THE NEW Blog\",\"url\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/author\\\/sakshi\\\/#author\",\"url\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/author\\\/sakshi\\\/\",\"name\":\"Sakshi Tyagi\",\"image\":{\"@type\":\"ImageObject\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/variable-hoisting-in-javascript\\\/#authorImage\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/a42c6deb088e18815dfcfed629c77b032d17bf973881421880b9c5e4ba713739?s=96&d=mm&r=g\",\"width\":96,\"height\":96,\"caption\":\"Sakshi Tyagi\"}},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/variable-hoisting-in-javascript\\\/#webpage\",\"url\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/variable-hoisting-in-javascript\\\/\",\"name\":\"Variable Hoisting In JavaScript | TO THE NEW Blog\",\"description\":\"Hoisting is a way in which JavaScript deals with variable and function declarations. What actually the JavaScript interpreter does is, it pushes all your Function and Variable declarations to the top of the containing scope.\",\"inLanguage\":\"en-US\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/#website\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/variable-hoisting-in-javascript\\\/#breadcrumblist\"},\"author\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/author\\\/sakshi\\\/#author\"},\"creator\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/author\\\/sakshi\\\/#author\"},\"datePublished\":\"2014-10-12T18:43:14+05:30\",\"dateModified\":\"2014-10-17T14:11:56+05:30\"},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/\",\"name\":\"TO THE NEW Blog\",\"inLanguage\":\"en-US\",\"publisher\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/#organization\"}}]}\n\t\t<\/script>\n\t\t<!-- All in One SEO -->\n\n","aioseo_head_json":{"title":"Variable Hoisting In JavaScript | TO THE NEW Blog","description":"Hoisting is a way in which JavaScript deals with variable and function declarations. What actually the JavaScript interpreter does is, it pushes all your Function and Variable declarations to the top of the containing scope.","canonical_url":"https:\/\/tothenewco.pro\/blog\/variable-hoisting-in-javascript\/","robots":"max-image-preview:large","keywords":"","webmasterTools":{"miscellaneous":""},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/tothenewco.pro\/blog\/variable-hoisting-in-javascript\/#article","name":"Variable Hoisting In JavaScript | TO THE NEW Blog","headline":"Variable Hoisting In JavaScript","author":{"@id":"https:\/\/tothenewco.pro\/blog\/author\/sakshi\/#author"},"publisher":{"@id":"https:\/\/tothenewco.pro\/blog\/#organization"},"datePublished":"2014-10-12T18:43:14+05:30","dateModified":"2014-10-17T14:11:56+05:30","inLanguage":"en-US","mainEntityOfPage":{"@id":"https:\/\/tothenewco.pro\/blog\/variable-hoisting-in-javascript\/#webpage"},"isPartOf":{"@id":"https:\/\/tothenewco.pro\/blog\/variable-hoisting-in-javascript\/#webpage"},"articleSection":"Technology, functions, hoisting, javascript, scopes, variables"},{"@type":"BreadcrumbList","@id":"https:\/\/tothenewco.pro\/blog\/variable-hoisting-in-javascript\/#breadcrumblist","itemListElement":[{"@type":"ListItem","@id":"https:\/\/tothenewco.pro\/blog#listItem","position":1,"name":"Home","item":"https:\/\/tothenewco.pro\/blog","nextItem":{"@type":"ListItem","@id":"https:\/\/tothenewco.pro\/blog\/category\/technology\/#listItem","name":"Technology"}},{"@type":"ListItem","@id":"https:\/\/tothenewco.pro\/blog\/category\/technology\/#listItem","position":2,"name":"Technology","item":"https:\/\/tothenewco.pro\/blog\/category\/technology\/","nextItem":{"@type":"ListItem","@id":"https:\/\/tothenewco.pro\/blog\/variable-hoisting-in-javascript\/#listItem","name":"Variable Hoisting In JavaScript"},"previousItem":{"@type":"ListItem","@id":"https:\/\/tothenewco.pro\/blog#listItem","name":"Home"}},{"@type":"ListItem","@id":"https:\/\/tothenewco.pro\/blog\/variable-hoisting-in-javascript\/#listItem","position":3,"name":"Variable Hoisting In JavaScript","previousItem":{"@type":"ListItem","@id":"https:\/\/tothenewco.pro\/blog\/category\/technology\/#listItem","name":"Technology"}}]},{"@type":"Organization","@id":"https:\/\/tothenewco.pro\/blog\/#organization","name":"TO THE NEW Blog","url":"https:\/\/tothenewco.pro\/blog\/"},{"@type":"Person","@id":"https:\/\/tothenewco.pro\/blog\/author\/sakshi\/#author","url":"https:\/\/tothenewco.pro\/blog\/author\/sakshi\/","name":"Sakshi Tyagi","image":{"@type":"ImageObject","@id":"https:\/\/tothenewco.pro\/blog\/variable-hoisting-in-javascript\/#authorImage","url":"https:\/\/secure.gravatar.com\/avatar\/a42c6deb088e18815dfcfed629c77b032d17bf973881421880b9c5e4ba713739?s=96&d=mm&r=g","width":96,"height":96,"caption":"Sakshi Tyagi"}},{"@type":"WebPage","@id":"https:\/\/tothenewco.pro\/blog\/variable-hoisting-in-javascript\/#webpage","url":"https:\/\/tothenewco.pro\/blog\/variable-hoisting-in-javascript\/","name":"Variable Hoisting In JavaScript | TO THE NEW Blog","description":"Hoisting is a way in which JavaScript deals with variable and function declarations. What actually the JavaScript interpreter does is, it pushes all your Function and Variable declarations to the top of the containing scope.","inLanguage":"en-US","isPartOf":{"@id":"https:\/\/tothenewco.pro\/blog\/#website"},"breadcrumb":{"@id":"https:\/\/tothenewco.pro\/blog\/variable-hoisting-in-javascript\/#breadcrumblist"},"author":{"@id":"https:\/\/tothenewco.pro\/blog\/author\/sakshi\/#author"},"creator":{"@id":"https:\/\/tothenewco.pro\/blog\/author\/sakshi\/#author"},"datePublished":"2014-10-12T18:43:14+05:30","dateModified":"2014-10-17T14:11:56+05:30"},{"@type":"WebSite","@id":"https:\/\/tothenewco.pro\/blog\/#website","url":"https:\/\/tothenewco.pro\/blog\/","name":"TO THE NEW Blog","inLanguage":"en-US","publisher":{"@id":"https:\/\/tothenewco.pro\/blog\/#organization"}}]},"og:locale":"en_US","og:site_name":"TO THE NEW BLOG","og:type":"blog","og:title":"Variable Hoisting In JavaScript | TO THE NEW Blog","og:description":"Hoisting is a way in which JavaScript deals with variable and function declarations. What actually the JavaScript interpreter does is, it pushes all your Function and Variable declarations to the top of the containing scope.","og:url":"https:\/\/tothenewco.pro\/blog\/variable-hoisting-in-javascript\/","og:image":"https:\/\/tothenewco.pro\/blog\/wp-content\/themes\/ttn\/images\/social-logo.png","og:image:secure_url":"https:\/\/tothenewco.pro\/blog\/wp-content\/themes\/ttn\/images\/social-logo.png","twitter:card":"summary","twitter:site":"@tothenew","twitter:title":"Variable Hoisting In JavaScript | TO THE NEW Blog","twitter:description":"Hoisting is a way in which JavaScript deals with variable and function declarations. What actually the JavaScript interpreter does is, it pushes all your Function and Variable declarations to the top of the containing scope.","twitter:image":"https:\/\/tothenewco.pro\/blog\/wp-content\/themes\/ttn\/images\/social-logo.png"},"aioseo_meta_data":{"post_id":"15722","title":null,"description":null,"keywords":null,"keyphrases":null,"primary_term":null,"canonical_url":null,"og_title":null,"og_description":null,"og_object_type":"default","og_image_type":"default","og_image_url":null,"og_image_width":null,"og_image_height":null,"og_image_custom_url":null,"og_image_custom_fields":null,"og_video":null,"og_custom_url":null,"og_article_section":null,"og_article_tags":null,"twitter_use_og":false,"twitter_card":"default","twitter_image_type":"default","twitter_image_url":null,"twitter_image_custom_url":null,"twitter_image_custom_fields":null,"twitter_title":null,"twitter_description":null,"schema":{"blockGraphs":[],"customGraphs":[],"default":{"data":{"Article":[],"Course":[],"Dataset":[],"FAQPage":[],"Movie":[],"Person":[],"Product":[],"ProductReview":[],"Car":[],"Recipe":[],"Service":[],"SoftwareApplication":[],"WebPage":[]},"graphName":"Article","isEnabled":true},"graphs":[]},"schema_type":null,"schema_type_options":null,"pillar_content":false,"robots_default":true,"robots_noindex":false,"robots_noarchive":false,"robots_nosnippet":false,"robots_nofollow":false,"robots_noimageindex":false,"robots_noodp":false,"robots_notranslate":false,"robots_max_snippet":null,"robots_max_videopreview":null,"robots_max_imagepreview":"large","priority":null,"frequency":null,"local_seo":null,"limit_modified_date":false,"created":"2021-04-30 07:58:09","updated":"2024-02-29 10:42:25","focus_keyword":null,"additional_keywords":null,"truseo_locale":null,"ai":null,"breadcrumb_settings":null,"seo_analyzer_scan_date":null},"aioseo_breadcrumb":"<div class=\"aioseo-breadcrumbs\"><span class=\"aioseo-breadcrumb\">\n\t\t\t<a href=\"https:\/\/tothenewco.pro\/blog\" title=\"Home\">Home<\/a>\n\t\t<\/span><span class=\"aioseo-breadcrumb-separator\">&raquo;<\/span><span class=\"aioseo-breadcrumb\">\n\t\t\t<a href=\"https:\/\/tothenewco.pro\/blog\/category\/technology\/\" title=\"Technology\">Technology<\/a>\n\t\t<\/span><span class=\"aioseo-breadcrumb-separator\">&raquo;<\/span><span class=\"aioseo-breadcrumb\">\n\t\t\tVariable Hoisting In JavaScript\n\t\t<\/span><\/div>","aioseo_breadcrumb_json":[{"label":"Home","link":"https:\/\/tothenewco.pro\/blog"},{"label":"Technology","link":"https:\/\/tothenewco.pro\/blog\/category\/technology\/"},{"label":"Variable Hoisting In JavaScript","link":"https:\/\/tothenewco.pro\/blog\/variable-hoisting-in-javascript\/"}],"_links":{"self":[{"href":"https:\/\/tothenewco.pro\/blog\/wp-json\/wp\/v2\/posts\/15722","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/tothenewco.pro\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/tothenewco.pro\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/tothenewco.pro\/blog\/wp-json\/wp\/v2\/users\/65"}],"replies":[{"embeddable":true,"href":"https:\/\/tothenewco.pro\/blog\/wp-json\/wp\/v2\/comments?post=15722"}],"version-history":[{"count":0,"href":"https:\/\/tothenewco.pro\/blog\/wp-json\/wp\/v2\/posts\/15722\/revisions"}],"wp:attachment":[{"href":"https:\/\/tothenewco.pro\/blog\/wp-json\/wp\/v2\/media?parent=15722"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/tothenewco.pro\/blog\/wp-json\/wp\/v2\/categories?post=15722"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/tothenewco.pro\/blog\/wp-json\/wp\/v2\/tags?post=15722"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}