{"id":4980,"date":"2024-11-19T23:41:41","date_gmt":"2024-11-19T18:11:41","guid":{"rendered":"https:\/\/namastedev.com\/blog\/?p=4980"},"modified":"2024-11-19T23:41:41","modified_gmt":"2024-11-19T18:11:41","slug":"understanding-how-var-const-let-and-functions-are-hoisted","status":"publish","type":"post","link":"https:\/\/namastedev.com\/blog\/understanding-how-var-const-let-and-functions-are-hoisted\/","title":{"rendered":"Understanding how var, const, let and functions are hoisted"},"content":{"rendered":"<p><\/p>\n<p><span class=\"ql-font-serif\">Have you heard of hoisting?, and might be wondering what it is right, or you probably got an error and they told you it was as a result of hoisting right?<\/span><\/p>\n<p><span class=\"ql-font-serif\">Anyways I am here to get you through it.<\/span><\/p>\n<p><\/p>\n<p><u class=\"ql-font-serif\">PREREQUISITE KNOWLEDGE:<\/u><\/p>\n<p><\/p>\n<p><span class=\"ql-font-serif\">JavaScript is a&nbsp;<\/span><em class=\"ql-font-serif\">synchronous single threaded <\/em><span class=\"ql-font-serif\"> language.<\/span><\/p>\n<p><strong class=\"ql-font-serif\">Synchronous<\/strong><span class=\"ql-font-serif\"> means it executes code in the order in which they were placed on the script (from the top to bottom), it&nbsp;executes the code from line 1, then line 2 and so on.<\/span><\/p>\n<p><\/p>\n<p><strong class=\"ql-font-serif\">Single threaded<\/strong><span class=\"ql-font-serif\"> means it&nbsp;only&nbsp;does one thing at a time, it does not multitask, it fully executes a line of code before it continues to the next line, it cannot be executing the code in line 3 at the same time the code in line 4.<\/span><\/p>\n<p><\/p>\n<p><span class=\"ql-font-serif\">So in short, synchronous single-threaded means it executes one line of code at a time in the order they appear on the script (from top to bottom).<\/span><\/p>\n<p><\/p>\n<p><\/p>\n<p><span class=\"ql-font-serif\">HOISTING is storing of variables and functions in the variable environment or memory of an execution context before execution.&nbsp;<\/span><\/p>\n<p><\/p>\n<p><span class=\"ql-font-serif\">Before JavaScript starts executing your code, it stores it memory first<\/span><\/p>\n<p><\/p>\n<p class=\"ql-indent-1\"><strong class=\"ql-size-large ql-font-serif\">1.<\/strong><span class=\"ql-font-serif\"> Variables stored with <\/span><em class=\"ql-font-serif\">\u201cvar\u201d<\/em><span class=\"ql-font-serif\"> keywords&nbsp;are assigned \u201cundefined\u201d in memory during hoisting. Hence if you try to access them before they are executed&nbsp;you will get undefined.<\/span><\/p>\n<p><\/p>\n<p><span class=\"ql-font-serif\"> NOTE: The debugger is turned on to pause to the code from executing at specific points so you can notice what happens within the code and the memory<\/span><img decoding=\"async\" src=\"https:\/\/namastedev.com\/blog\/wp-content\/uploads\/2024\/09\/Screenshot-1330-1.png\"><\/p>\n<p><\/p>\n<p><span class=\"ql-font-serif\">Notice&nbsp;what is assigned to \u201ca\u201d in the global memory before&nbsp;the line var a = \u201cJavaScript\u201d was executed.<\/span><\/p>\n<p><\/p>\n<p class=\"ql-indent-1\"><span class=\"ql-size-large ql-font-serif\">2. <\/span><span class=\"ql-font-serif\">Variables stored with <\/span><em class=\"ql-font-serif\">\u201cconst\u201d<\/em><span class=\"ql-font-serif\"> and <\/span><em class=\"ql-font-serif\">\u201clet<\/em><span class=\"ql-font-serif\">\u201d keywords assigned \u201c&lt;value unavailable&gt;\u201d are stored in the Temporal Dead Zone (TDZ) , a separate section within the global memory. When you try to access them before they are executed, you get an error \u201cReferenceError: Cannot access &#8216;b&#8217; before initialization\u201d.<\/span><\/p>\n<p><img decoding=\"async\" src=\"https:\/\/namastedev.com\/blog\/wp-content\/uploads\/2024\/09\/Screenshot-1333.png\"><\/p>\n<p><\/p>\n<p><\/p>\n<p><span class=\"ql-font-serif\">Notice variables \u201ca\u201d and \u201cb\u201d are saved in \u201cScript\u201d and not \u201cGlobal\u201d and they are assigned&nbsp;\u201c&lt;value unavailable&gt;\u201d. This only happens before line const b =\u201dHoisting\u201d and let c = \u201cJohn\u201d are executed.<\/span><\/p>\n<p><em class=\"ql-font-serif\">Classes<\/em><span class=\"ql-font-serif\"> are also hoisted like <\/span><em class=\"ql-font-serif\">\u201cconst\u201d<\/em><span class=\"ql-font-serif\"> and <\/span><em class=\"ql-font-serif\">\u201clet\u201d<\/em><span class=\"ql-font-serif\">. You cannot initialize a class before executing it else you will get an error<\/span><\/p>\n<p><span class=\"ql-font-serif\">Example:&nbsp;<\/span><\/p>\n<p><img decoding=\"async\" src=\"https:\/\/namastedev.com\/blog\/wp-content\/uploads\/2024\/09\/Screenshot-1339.png\"><\/p>\n<p><\/p>\n<p><\/p>\n<p><\/p>\n<p class=\"ql-indent-1\"><strong class=\"ql-size-large ql-font-serif\">3.<\/strong><span class=\"ql-font-serif\"> Functions are stored with a reference to the entire function code, hence you can invoke or access them before they are executed.<\/span><\/p>\n<p><img decoding=\"async\" src=\"https:\/\/namastedev.com\/blog\/wp-content\/uploads\/2024\/09\/Screenshot-1336.png\"><\/p>\n<p><\/p>\n<p><span class=\"ql-font-serif\">Notice here that the function labeled \u201c<\/span><em class=\"ql-font-serif\">arr\u201d <\/em><span class=\"ql-font-serif\">has been saved in memory before it is executed, the debugger is placed on the first line <\/span> <span class=\"ql-font-serif\">which means it pauses the code execution.<\/span><\/p>\n<p><\/p>\n<p><span class=\"ql-font-serif\">Hence you can easily use or print or invoke a function before it is executed in the next line of code.&nbsp;<\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Have you heard of hoisting?, and might be wondering what it is right, or you probably got an error and they told you it was as a result of hoisting right? Anyways I am here to get you through it. PREREQUISITE KNOWLEDGE: JavaScript is a&nbsp;synchronous single threaded language. Synchronous means it executes code in the<\/p>\n","protected":false},"author":50,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[230,172],"tags":[330],"class_list":["post-4980","post","type-post","status-publish","format-standard","category-chosen-topic","category-javascript","tag-javascript"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/posts\/4980","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/users\/50"}],"replies":[{"embeddable":true,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/comments?post=4980"}],"version-history":[{"count":1,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/posts\/4980\/revisions"}],"predecessor-version":[{"id":5002,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/posts\/4980\/revisions\/5002"}],"wp:attachment":[{"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/media?parent=4980"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/categories?post=4980"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/tags?post=4980"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}