{"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":18878,"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":"61f8ef79e12bce537863b4cf","content":{"languages":["javascript","typescript"],"difficulty":4},"tags":["javascript","walmart","frontend interview question","devtools","ui","advanced javascript"],"slug":"implement-throttling-in-javascript-or-walmart-frontend-interview-question---qid---mLrwAyug9CgrVGbs6OYF","title":"Implement Throttling in JavaScript | Walmart Frontend Interview Question","questionId":"mLrwAyug9CgrVGbs6OYF"},{"_id":"63187a5677f9961d5b7d0f2b","content":{"languages":["javascript","typescript"],"difficulty":2},"tags":["javascript","array","reduce","polyfill","code","devtools tech","frontend problem solving","js interview questions","js roadmap","interview roadmap","advanced js","interview preparation","frontend masters","codedamn"],"slug":"implement-reduce-polyfill-from-scratch-or-frontend-problem-solving-or-javascript-interview-question---qid---yGnWTFf04zUUH6guACIJ","title":"Implement Reduce Polyfill from Scratch | Frontend Problem Solving | JavaScript Interview Question","questionId":"yGnWTFf04zUUH6guACIJ"},{"_id":"66daa0dd20aedf09de81ee98","content":{"languages":["javascript","typescript"],"difficulty":2},"tags":["frontend","ui","ux","devtools tech","problem solving","lodash","polyfills","frontend practice"],"slug":"implement-count-by-or-javascript-problem-solving---qid---ajVW4hL2Zz5DeDCl37uv","title":"Implement Count By | JavaScript Problem Solving","questionId":"ajVW4hL2Zz5DeDCl37uv"},{"_id":"6215e6591641361c7c65de90","content":{"difficulty":3,"languages":"javascript"},"tags":["javascript","frontend","coding","architecture","js fundamentals","web","system design"],"slug":"how-would-you-implement-pagination-in-a-frontend-application-or-javascript-interview-questions-or-frontend-architecture---qid---IL8Whw2ayeqmg0kvHQLd","title":"How would you implement pagination in a frontend application? | JavaScript Interview Questions | Frontend Architecture","questionId":"IL8Whw2ayeqmg0kvHQLd"},{"_id":"601906d20858864c592fb52c","content":{"difficulty":4,"languages":"javascript"},"tags":["javascript","interview","js fundamentals","object destructing","variable assignment"],"slug":"what-would-be-the-output-object-destructuring---qid---4Eq7UEhC4KZxnYZOFOjN","title":"What would be the output? [Object Destructuring]","questionId":"4Eq7UEhC4KZxnYZOFOjN"}],"resources":[{"_id":"620a50051641361c7c65bd61","content":{"difficulty":4,"domain":2,"type":1,"isInternal":true},"tags":[""],"slug":"how-to-fix-errors-like-referenceerror-window-is-not-defined-in-remix-powered-web-app-or-javascript-frameworks---rid---G0OTehUXo6QQCrfykSF3","title":"How to fix errors like ReferenceError Window is Not Defined in Remix Powered Web App | JavaScript Frameworks","resourceId":"G0OTehUXo6QQCrfykSF3"},{"_id":"5f1dd80bcbec5f7ffc0c2faf","content":{"difficulty":2,"domain":3,"type":1,"isInternal":true},"tags":["node.js","javascript","bots","automation","puppeteer","browser automation","ui testing","web scraping","linkedin scraping","devtools","ui"],"slug":"tips-to-improve-browser-automation-bots---rid---UVxuatGCbZwSMoQtQtaS","title":"Tips to improve Browser Automation Bots","resourceId":"UVxuatGCbZwSMoQtQtaS"},{"_id":"6a12f5ac4b3e6d922c54a48c","content":{"difficulty":1,"domain":1,"type":1,"isInternal":true,"languages":[]},"tags":["javascript","ui","ux","devtools tech","coding","frontend","lazy loading images","responsive images","web performance"],"slug":"lazy-loading-images---rid---YdibwFfrwv3NjOL8RtUV","title":"Lazy Loading Images","resourceId":"YdibwFfrwv3NjOL8RtUV"},{"_id":"6963e277132ce4e954b04414","content":{"difficulty":4,"domain":2,"type":1,"isInternal":true,"languages":[]},"tags":["javascript","ui","ux","devtools tech","coding","frontend performance","browser rendering cycle"],"slug":"mastering-critical-rendering-path---rid---VyhOczhtA0Ds1kp2s4TN","title":"Mastering Critical Rendering Path","resourceId":"VyhOczhtA0Ds1kp2s4TN"},{"_id":"69a52acc92f33c7d8a9f6107","content":{"difficulty":4,"domain":1,"type":2,"isInternal":false,"languages":[]},"tags":["javascript","ui","ux","devtools tech","tutorial","rendering","coding"],"slug":"how-react-ssr-actually-works-without-frameworks---rid---8decG2azJUycVwuG2WQE","title":"How React SSR Actually Works (Without Frameworks)","resourceId":"8decG2azJUycVwuG2WQE"}]}}