{"id":136270,"date":"2024-01-31T12:34:43","date_gmt":"2024-01-31T12:34:43","guid":{"rendered":"https:\/\/www.sushilkumar.ind.in\/blog\/?p=136270"},"modified":"2024-01-31T13:03:02","modified_gmt":"2024-01-31T13:03:02","slug":"what-is-hoisting-in-javascript-with-example","status":"publish","type":"post","link":"https:\/\/www.sushilkumar.ind.in\/blog\/javascript\/what-is-hoisting-in-javascript-with-example\/","title":{"rendered":"What is hoisting in javascript with example?"},"content":{"rendered":"\n<p>Hoisting in JavaScript is a behavior where variable and function declarations are moved to the top of their containing scope during the compilation phase, regardless of where they are declared in the code. This means that you can access variables and functions before they are declared in your code.<\/p>\n\n\n\n<p>However, it&#8217;s important to understand that only the declarations are hoisted, not the initializations or assignments. Here are a few key points about hoisting:<\/p>\n\n\n\n<p>1. <strong>**Variable Declarations:**<\/strong> When you declare a variable using `var`, `let`, or `const`, the declaration is hoisted to the top of its containing function or global scope. If you try to access the variable before its declaration, it will exist but have the value `undefined`.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>   console.log(x); \/\/ Output: undefined\n   var x = 10;\n\n2. **Function Declarations:** Function declarations are also hoisted to the top of their containing scope. This means you can call a function before it appears in the code.<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>   greet(); \/\/ Output: \"Hello, World!\"\n\n   function greet() {\n       console.log(\"Hello, World!\");\n   }<\/code><\/pre>\n\n\n\n<p>3. <strong>**Function Expressions:**<\/strong> However, function expressions (where a function is assigned to a variable) are not hoisted in the same way. Only the variable declaration is hoisted, not the function assignment.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>   greet(); \/\/ This will throw an error\n\n   var greet = function() {\n       console.log(\"Hello, World!\");\n   };<\/code><\/pre>\n\n\n\n<p><strong>Here are a few more examples to illustrate hoisting in JavaScript:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>console.log(x); \/\/ Output: undefined\nvar x = 10;\nconsole.log(x); \/\/ Output: 10<\/code><\/pre>\n\n\n\n<p>In this example, the declaration of the variable x is hoisted to the top of the scope, but the initialization (var x = 10;) remains in place. Therefore, when console.log(x) is executed before the initialization, x exists but has the value undefined.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>var x = 10;\n\nfunction example() {\n    console.log(x); \/\/ Output: undefined\n    var x = 20;\n    console.log(x); \/\/ Output: 20\n}\n\nexample();\nconsole.log(x); \/\/ Output: 10\n<\/code><\/pre>\n\n\n\n<p>In this example, within the example function, the declaration var x = 20; is hoisted to the top of the function&#8217;s scope, but not its initialization. Therefore, the first console.log(x) prints undefined. The x within the function is a different variable than the global x.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hoisting in JavaScript is a behavior where variable and function declarations are moved to the top of their containing scope during the compilation phase, regardless of where they are declared in the code. This means that you can access variables and functions before they are declared in your code. However, it&#8217;s important to understand that &hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","enabled":false},"version":2}},"categories":[323],"tags":[],"class_list":["post-136270","post","type-post","status-publish","format-standard","","category-javascript"],"jetpack_publicize_connections":[],"acf":[],"aioseo_notices":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p99pkJ-zrU","_links":{"self":[{"href":"https:\/\/www.sushilkumar.ind.in\/blog\/wp-json\/wp\/v2\/posts\/136270"}],"collection":[{"href":"https:\/\/www.sushilkumar.ind.in\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.sushilkumar.ind.in\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.sushilkumar.ind.in\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.sushilkumar.ind.in\/blog\/wp-json\/wp\/v2\/comments?post=136270"}],"version-history":[{"count":3,"href":"https:\/\/www.sushilkumar.ind.in\/blog\/wp-json\/wp\/v2\/posts\/136270\/revisions"}],"predecessor-version":[{"id":136273,"href":"https:\/\/www.sushilkumar.ind.in\/blog\/wp-json\/wp\/v2\/posts\/136270\/revisions\/136273"}],"wp:attachment":[{"href":"https:\/\/www.sushilkumar.ind.in\/blog\/wp-json\/wp\/v2\/media?parent=136270"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.sushilkumar.ind.in\/blog\/wp-json\/wp\/v2\/categories?post=136270"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.sushilkumar.ind.in\/blog\/wp-json\/wp\/v2\/tags?post=136270"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}