{"resource":{"author":{"id":"h2so9H8jPMmgUKGghoNl","name":"Yomesh Gupta","username":"yomeshgupta"},"content":{"link":null,"difficulty":1,"domain":2,"type":1,"isInternal":true,"body":"JavaScript is one of the most popular languages. There are a lot of resources on the web teaching JavaScript and its concepts. Our aim with this series is to share a concise, useful, and quality list of questions with their answers. We don't want to add to the noise but rather be the source of clarity.\r\n\r\n### Explain Hoisting in javascript\r\n\r\nThe most common explanation of Hoisting on the web would be\r\n\r\n> Hoisting is the default behaviour of JavaScript where all the variables and functions declarations are moved to the top.\r\n\r\nIt is correct but there is more to it than what meets the eye.\r\n\r\n### Variable Declarations\r\n\r\nIn JavaScript, we can define variables using different keywords that have subtle differences among them [https://www.ecma-international.org/ecma-262/6.0/#sec-let-and-const-declarations](https://www.ecma-international.org/ecma-262/6.0/#sec-let-and-const-declarations).\r\n\r\nWe can declare variables in three ways --\r\n\r\n```js\r\nvar name = \"Yomesh\";\r\nlet surname = \"Gupta\";\r\nconst profession = \"Senior Frontend Engineer\";\r\n```\r\n\r\nJavaScript engine runs through our code in two phases — the `Creation Phase` and the `Execution Phase`. In the first (Creation) phase, the engine goes through the code and allocates memory for the variables.\r\n\r\nIn the same phase, the variables declared via `var` are assigned the value `undefined` and that is why in the below code snippet you will get the value of the variable `firstname` as `undefined`.\r\n\r\n```js\r\nconsole.log(firstname); // undefined\r\nvar firstname = \"Yomesh\";\r\n```\r\n\r\nThis is what we know as `Hoisting`.\r\n\r\nIn the case of the variables declared via `let` and `const`, they are also allocated memory but they are not assigned the value `undefined` initially i.e. variable declarations do hoist but they throw an error till initialized. Let us consider an example.\r\n\r\n```js\r\n/*\r\n  Since variables are hoisted\r\n  it would be equivalent to\r\n\r\n  let firstname;\r\n*/\r\n// ReferenceError as memory is allocated but undefined is not assigned initially\r\nconsole.log(firstname);\r\n\r\nfunction init() {\r\n...\r\n}\r\nfunction processing() {\r\n...\r\n}\r\n\r\nlet firstname = \"Yomesh\"; // initialization\r\nconsole.log(firstname); // Yomesh\r\n```\r\n\r\nIn the Execution phase, variables are assigned their actual values and it is perfectly fine to use them afterwards.\r\n\r\n```js\r\nconst name = \"Yomesh\";\r\nconsole.log(name); // Yomesh | Perfectly fine to use here\r\n```\r\n\r\n### Temporal Dead Zone\r\n\r\nLet us re-consider one of the previously discussed examples again.\r\n\r\n```js\r\n// Beginning of the temporal dead zone\r\nconsole.log(firstname); // ReferenceError\r\n\r\nfunction init() {\r\n...\r\n}\r\nfunction processing() {\r\n...\r\n}\r\n\r\nlet firstname = \"Yomesh\"; // Ending of the temporal dead zone\r\nconsole.log(firstname); // Yomesh\r\n```\r\n\r\nAs we have seen earlier, any variable declared via `let` or `const` cannot be accessed before their `initialization`. The region for the variable `firstname` which begins from the place where the variable is hoisted as in its scope is created till the place where the variable is initialized is known as `Temporal Dead Zone`. Temporal Dead Zone is a good way to indicate bugs in the code. Accessing variables before the declaration is often a root cause of many errors.\r\n\r\n### Hoisting in Functions\r\n\r\nHoisting in functions works the same way with the only notable exception that function declarations are hoisted but not function expressions. For example --\r\n\r\n```js\r\nsayHello(); // logs Hello\r\n\r\n// Function declaration\r\nfunction sayHello() {\r\n  console.log(\"Hello\");\r\n}\r\n```\r\n\r\nFunction declarations are hoisted to the top of the enclosing function or global scope. We can use the function before we declared it.\r\n\r\n```js\r\nsayHello(); // Uncaught TypeError: sayHello is not a function\r\n\r\n// Function expression\r\nvar sayHello = function () {\r\n  console.log(\"Hello\");\r\n};\r\n```\r\n\r\nFunction expressions are not hoisted, unlike function declarations. We can't use function expressions before we create them.\r\n\r\nI hope this was helpful to you in some way. Please do share with others so that they can learn too! :D\r\n","languages":[],"editorConfig":{}},"stats":{"views":18877,"used":0,"likes":0},"description":"","published":true,"isActive":true,"tags":["javascript","hoisting","interview","js fundamentals","frontend fundamentals","scoping","temporal dead zone","frontend interview questions","interview answers"],"slug":"hoisting-in-javascript-explained-or-javascript-interview-questions---rid---6oy63tirhGHUyvC3HYf1","isPremium":false,"categories":[],"requires":[],"_id":"61f104fcf144a82a5ee29fb2","title":"Hoisting in JavaScript Explained | JavaScript Interview Questions","resourceId":"6oy63tirhGHUyvC3HYf1","createdAt":1643185404922,"modifiedAt":1643966497881},"currentUser":null,"isOwner":false,"recommendations":{"questions":[{"_id":"67cc043127b2785b41f18eb0","content":{"languages":["react","html"],"difficulty":2},"tags":["javascript","ui","ux","interview","drag and drop","questions","interview questions","frontend interview"],"slug":"how-to-create-a-drag-and-drop-list-component---qid---C5yqJ5JJaTUY6en6Bs1R","title":"How to create a Drag and Drop List Component?","questionId":"C5yqJ5JJaTUY6en6Bs1R"},{"_id":"60ce47a8e541233080dcd5c5","content":{"languages":["react"],"difficulty":4},"tags":["javascript","react","react hooks","frontend","javascript fundamentals","interview questions","frontend fundamentals"],"slug":"how-to-build-a-custom-timer-hook-in-react-js-or-usetimer-or-javascript-interview-question---qid---H5KlkIeowa1LrIn1mSN5","title":"How to build a custom timer hook in React.js? | useTimer | JavaScript Interview Question","questionId":"H5KlkIeowa1LrIn1mSN5"},{"_id":"67766b931216f11205475648","content":{"languages":["javascript","typescript"],"difficulty":2},"tags":["javascript","frontend","coding","ui","ux","devtools tech","atlassian","fe","programming","interview question","async"],"slug":"how-to-create-analytics-sdk-in-javascript---qid---DgfGnCyNDx2BHmHeEtzb","title":"How to create analytics SDK in JavaScript?","questionId":"DgfGnCyNDx2BHmHeEtzb"},{"_id":"5ec557165b57cb40dd43fa35","content":{"difficulty":1,"languages":"javascript"},"tags":["node.js","javascript","algorithms","dsa","time complexity","interview preparation","cs fundamentals"],"slug":"what-is-the-time-complexity-of-the-following-code-snippet---qid---G3yPO9NqbXIkpWdVQ9od","title":"What is the time complexity of the following code snippet?","questionId":"G3yPO9NqbXIkpWdVQ9od"},{"_id":"675005761216f112053d2b9d","content":{"languages":["javascript","typescript"],"difficulty":1},"tags":["javascript","array","linkedin","question","frontend","ui","ux","problem solving","interview question","tutorial","blog"],"slug":"how-to-implement-custom-array-prototype-square-method---qid---D2G7RkqdWxAjck6U2NV7","title":"How to implement custom Array.prototype.square method?","questionId":"D2G7RkqdWxAjck6U2NV7"}],"resources":[{"_id":"6339359342e7761bfac16e89","content":{"difficulty":1,"domain":2,"type":2,"isInternal":false},"tags":["javascript","requestly","frontend","devtools tech","coding","mock server","mock api","headers","redirect","api"],"slug":"how-to-supercharge-your-development-workflow-or-reviewing-requestlyio-or-improving-frontend-dx---rid---vgQNMcZkgncuDninBiYf","title":"How to Supercharge Your Development Workflow!? | Reviewing Requestly.io | Improving Frontend DX","resourceId":"vgQNMcZkgncuDninBiYf"},{"_id":"5f202d38cbec5f7ffc0c2fbc","content":{"difficulty":1,"domain":2,"type":2},"tags":["Slice"," Splice"," Javascript ","Interview Questions "],"slug":"slice-and-splice-oror-javascript-interview-questions-oror-puneet-ahuja---rid---vJoGsYEHYTzhQDdvQJ4O","title":"Slice and Splice || Javascript Interview Questions || Puneet Ahuja","resourceId":"vJoGsYEHYTzhQDdvQJ4O"},{"_id":"62056636e12bce537863f36a","content":{"difficulty":1,"domain":1,"type":1,"isInternal":true},"tags":["javascript","remix run","social login","login with google","session","encryption","js fundamentals","remix server"],"slug":"add-social-authentication-to-remix-run-project-or-remix-server---rid---GEN4IbgWorJNeQL7Gkm8","title":"Add Social Authentication to Remix Run Project | Remix Server","resourceId":"GEN4IbgWorJNeQL7Gkm8"},{"_id":"5f266204cbec5f7ffc0c2fc2","content":{"difficulty":1,"domain":2,"type":1},"tags":["javascript","es6","js fundamentals","arrow functions","tania rascia","devtools"],"slug":"understanding-arrow-functions-in-javascript---rid---RZqVVZQ1dO8GwrecJbft","title":"Understanding Arrow Functions in JavaScript","resourceId":"RZqVVZQ1dO8GwrecJbft"},{"_id":"655600ac3e4c0459a44b430d","content":{"difficulty":4,"domain":2,"type":1,"isInternal":false,"languages":["javascript"]},"tags":["frontend","coding","ui","ux","razorpay","interview experience","tooling","devtools tech","hasnode","blog","tutorial"],"slug":"razorpay-frontend-engineer-interview-experience---rid---1JDZUAHqg0r2uYXxVPZW","title":"Razorpay Frontend Engineer interview experience","resourceId":"1JDZUAHqg0r2uYXxVPZW"}]}}