{"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":19582,"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":"63a97bea22480f26b3cbb6ad","content":{"languages":["javascript","typescript"],"difficulty":1},"tags":["javascript","problem solving","interview question","interview preparation","function","toggle functionality","vanilla js","basic js","facebook interview","netflix","google interview questions","frontend interview questions"],"slug":"how-to-create-a-toggle-function-in-javascript-or-frontend-problem-solving-or-javascript-interview-question---qid---gbucvY2jab4e1q6Fo6iV","title":"How to create a toggle function in JavaScript? | Frontend Problem Solving | JavaScript Interview Question","questionId":"gbucvY2jab4e1q6Fo6iV"},{"_id":"5fce64866d3cda64e470c420","content":{"languages":["javascript"],"difficulty":4},"tags":["javascript","new operator","frontend","js fundamentals","frontend fundamentals","interview concepts"],"slug":"what-would-be-the-output-of-the-following-code-snippet-es6-classes---qid---212yoqEY48ZvHTpK2zQI","title":"What would be the output of the following code snippet? [ES6 Classes]","questionId":"212yoqEY48ZvHTpK2zQI"},{"_id":"65ad10f98f7afe4225332ec3","content":{"languages":["react","html"],"difficulty":4},"tags":["javascript","frontend","code","ui","ux","devtools tech","frontend coding challenge","react","ui game","interview question","ms"],"slug":"build-country-capital-game-or-microsoft-frontend-interview-question-or-javascript-or-react-js---qid---yPb5g7MLCSf6j2F3qjqj","title":"Build Country Capital Game | Microsoft Frontend Interview Question | JavaScript | React.js","questionId":"yPb5g7MLCSf6j2F3qjqj"},{"_id":"6878f2a950393ab55083191e","content":{"languages":["javascript","typescript"],"difficulty":1},"tags":["javascript","objects","frontend interview questions","ui","ux","devtools tech","coding","tutorial","blog","zepto coding challenge"],"slug":"implement-listento-function---qid---JqRACUtqsOONwFUNREXb","title":"Implement listenTo function","questionId":"JqRACUtqsOONwFUNREXb"},{"_id":"693e85d77a8f644500329fb6","content":{"languages":["react"],"difficulty":2},"tags":["javascript","frontend","ui","ux","devtools tech","frontend interview challenge","coding question"],"slug":"cursor-tracker---qid---ATU3sJ5FPYzRkAEweNuS","title":"Cursor Tracker","questionId":"ATU3sJ5FPYzRkAEweNuS"}],"resources":[{"_id":"6310505a77f9961d5b7ce983","content":{"difficulty":2,"domain":1,"type":2,"isInternal":false},"tags":["javascript","redux","react","youtube","interviews","frontend","interview preparation","tooling","state management"],"slug":"build-your-own-redux-or-part-1-or-advanced-frontend---rid---MNkFAzvLnmds66wD3JV0","title":"Build Your Own Redux | Part 1 | Advanced Frontend","resourceId":"MNkFAzvLnmds66wD3JV0"},{"_id":"5f1ffa3acbec5f7ffc0c2fb7","content":{"difficulty":3,"domain":2,"type":1},"tags":["javascript","frontend","advanced react","build react","react internals","tutorials","reactjs"],"slug":"build-your-own-react---rid---XdoSkFr67KA5TMwu1lc0","title":"Build your own React","resourceId":"XdoSkFr67KA5TMwu1lc0"},{"_id":"5f62fc016d3cda64e470c3f3","content":{"difficulty":4,"domain":2,"type":2},"tags":["javascript","closures","frontend","js fundamentals","devtools","interview concept"],"slug":"javascript-closures-or-lexical-environment-or-interview-question-or-saloni-kathuria---rid---OlEK5KKN3yZeASgPJfTm","title":"JavaScript Closures | Lexical Environment | Interview Question | Saloni Kathuria","resourceId":"OlEK5KKN3yZeASgPJfTm"},{"_id":"638843af937c6f549165e2cb","content":{"difficulty":4,"domain":1,"type":2,"isInternal":false},"tags":["javascript","frontend","code","programming","system design","coding","interivews","advanced js"],"slug":"how-to-generate-dynamic-social-preview-images-or-frontend-system-design-or-javascript-problem-solving---rid---NpZl2oRSbaFEMQLfo9uG","title":"How to Generate Dynamic Social Preview Images? | Frontend System Design | JavaScript Problem Solving","resourceId":"NpZl2oRSbaFEMQLfo9uG"},{"_id":"6967584e132ce4e954b254eb","content":{"difficulty":4,"domain":2,"type":1,"isInternal":true,"languages":[]},"tags":["javascript","ui","ux","devtools tech","coding","blinkit","interview experience","frontend"],"slug":"blinkit-frontend-interview-experience-sde-1---rid---j7MSrpMfo09YVcBzRXYa","title":"Blinkit Frontend Interview Experience (SDE 1)","resourceId":"j7MSrpMfo09YVcBzRXYa"}]}}