{"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":19571,"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":"6a6658afda1016b2d8055e44","content":{"languages":["react"],"difficulty":2},"tags":["javascript","ui","ux","devtools tech","coding","frontend challenge","coding","interview question"],"slug":"selectable-grid-cells---qid---Y2el5BHY0tMLNgqFW0n7","title":"Selectable Grid Cells","questionId":"Y2el5BHY0tMLNgqFW0n7"},{"_id":"6a4bd7de9d0d362116223e23","content":{"languages":["react","html"],"difficulty":3},"tags":["javascript","ui","ux","devtools tech","coding","frontend interview question","query building"],"slug":"query-builder---qid---TlxtqhI8ZcNeZ0i1FloX","title":"Query Builder","questionId":"TlxtqhI8ZcNeZ0i1FloX"},{"_id":"636cdeb520622762d08cff03","content":{"languages":["javascript"],"difficulty":1},"tags":["javascript","frontend","coding","devtools tech","interview questions","interview preparation","mcq","programming paradigm","tooling","mdn","js paradigm","programming questions","variables","let","const","var"],"slug":"which-of-the-following-keywords-are-used-to-define-a-variable-in-javascript---qid---WbDSiPnjbvPsb5iKKWJw","title":"Which of the following keywords are used to define a variable in JavaScript?","questionId":"WbDSiPnjbvPsb5iKKWJw"},{"_id":"642be8c76577005718b848a8","content":{"languages":["html","react"],"difficulty":1},"tags":["frontend","ui","ux","progress bars","uber","frontend interview question","reactjs","devtools tech","system design","frontend masters","coding","javascript","advanced javascript"],"slug":"how-to-animate-multiple-progress-bars-in-a-sequence-or-uber-frontend-interview-question-or-react-js-or-javascript-or-html-or-css---qid---0pJDOORWvcix4ymghsB9","title":"How to animate multiple progress bars in a sequence? | Uber Frontend Interview Question | React.js | JavaScript | HTML | CSS","questionId":"0pJDOORWvcix4ymghsB9"},{"_id":"636cdb1620622762d08cfe50","content":{"languages":["javascript"],"difficulty":1},"tags":["javascript","frontend","coding","devtools tech","interview questions","interview preparation","mcq","programming paradigm","tooling","mdn","js paradigm","programming questions"],"slug":"what-is-the-paradigm-of-javascript-language---qid---MALYnvxIvkLPYvYuEFjj","title":"What is the paradigm of JavaScript language?","questionId":"MALYnvxIvkLPYvYuEFjj"}],"resources":[{"_id":"5fb4d1456d3cda64e470c40c","content":{"difficulty":2,"domain":2,"type":2},"tags":["javascript","frontend","node.js","dom","frontend fundamentals","interview questions"],"slug":"dom-api-programming-question-or-frontend-interview-questions---rid---AA4msQuDtbwicYNMLOt3","title":"DOM API Programming Question | Frontend Interview Questions","resourceId":"AA4msQuDtbwicYNMLOt3"},{"_id":"69824d07895fc3bf013b40e2","content":{"difficulty":4,"domain":1,"type":1,"isInternal":true,"languages":[]},"tags":["javascript","ui","ux","blog","best practices","code","devtools tech","article"],"slug":"best-code-practices-for-real-world-applications-part-1---rid---8ohjaCmxsxK093sIQad6","title":"Best Code Practices for Real-World Applications - Part 1","resourceId":"8ohjaCmxsxK093sIQad6"},{"_id":"5f202c81cbec5f7ffc0c2fba","content":{"difficulty":1,"domain":2,"type":1},"tags":["Web Development"],"slug":"script-tag-—-defer-and-async---rid---NCXH57kUsnTM2Ev2tzEU","title":"Script Tag — Defer and Async","resourceId":"NCXH57kUsnTM2Ev2tzEU"},{"_id":"6a12e5684b3e6d922c54a0e9","content":{"difficulty":1,"domain":1,"type":1,"isInternal":true,"languages":[]},"tags":["javascript","ui","ux","devtools tech","tutorials","blogs","react","image optimisation","responsive images","web performance"],"slug":"responsive-images-srcset-sizes-and-picture---rid---YlLGKU8oqpo6ivZBG2mI","title":"Responsive Images: srcset, sizes, and picture","resourceId":"YlLGKU8oqpo6ivZBG2mI"},{"_id":"625939ff1195627fe9f0bb9c","content":{"difficulty":4,"domain":17,"type":3,"isInternal":false},"tags":["life","podcast","frontend","backend.software development","tooling","devtools tech","ios","fueled","ronnie and barty","grappus","fullstack"],"slug":"beyond-code-or-episode-1-or-video-podcast---rid---wituSBDrHHWE5yB7mq7R","title":"Beyond Code | Episode 1 | Video Podcast","resourceId":"wituSBDrHHWE5yB7mq7R"}]}}