JavaScript Hoisting Interview Questions | Coding Exercise

hoisting interview questions, javascript interview questions, javascript hoisting interview questions, javascript hoisting tricky questions, hoisting in let and const, javascript coding exercise
š” JavaScript Hoisting
is a process to add variables declarations and function declarations to the top of memory inside the JavaScript Data Structure during compile time.
That data structure is called Lexical Environment
which holds data in key-value mapping.
console.log(person); var person = 'This is var'; // output > undefined fun(); function fun() { console.log('This is fun'); } // output > This is fun.
As we know that function declaration is hoisted means these are added first in memory that’s why we are able to access them before the actual declaration.
š” Keep In mind, Function expressions are not hoisted.
printMe(); var printMe = function() { console.log('this is expression') } // output > Uncaught TypeError: printMe is not a function
š” let and const variable
console.log(a); let a = 10; output > ReferenceError: Cannot access 'a' before initialization let b; console.log(b); b = 12; output > undefined
š” So the Question is – are let and const variables not hoisted?
- In JavaScript,
All declarations (function, var, let, const, and class) are hoisted
, while the var declarations are initialized with undefined, but let and const declarations remain uninitialized. - let & const only get initialized when an expression is evaluated during runtime by the JavaScript engine.
function foo () { console.log(x); } let x = 20; foo(); output > 20 // because let is initialized in window scope.
š” JavaScript Classes
let personObj = new Person("JSMount"); console.log(personObj); class Person { constructor(name) { this.name = name; } } output > Uncaught ReferenceError: Cannot access 'Person' before initialization
Same as let and const, classes are also hoisted but they remain uninitialized until execution.
So to access the classes, We have to declare them first.
****************************
Let’s see some Console Input Output Programs –
function parent() { var hoisted = "'Iām a variable"; function hoisted() { return "Iām a function"; } return hoisted(); } console.log(parent()); output > Uncaught TypeError: hoisted is not a function
function parent() { return hoisted(); var hoisted = "'Iām a variable"; function hoisted() { return "Iām a function"; } } console.log(parent()); output > "Iām a function";
function parent() { var hoisted = "'Iām a variable"; var hoisted = function hoisted() { return "Iām a function"; } return hoisted(); } console.log(parent()); output > 'I am a function'
function emp() { console.log(ā111ā); } emp(); function emp() { console.log(ā2ā); } Output > 2
var emp = function() { console.log(ā1ā); } emp(); function emp() { console.log(ā2ā); } emp(); output > 1 1
var x = 5; console.log(x); var x; output > 5
var y = 10; var y; console.log(y); output > 10
var y = 10; var y = 12; console.log(y); output > 12
var z = 21; let z; console.log(z); output > Identifier āzā has already been declared
let y = 12; console.log(y); var y = 11; output > Identifier āyā has already been declared
var a = 5; console.log(a); var a = 7; console.log(a); output > 5 7
console.log(y); y = 15; output > ReferenceError: y is not defined
var x = 1; console.log(x, y); var y; Output > 1 undefined
var x = 1; console.log(x, y); var y; delete x; delete y; console.log(x, y); output > will be same for both : 1 undefined
x = 1; console.log(x, y); var y; delete x; delete y; console.log(x, y); output > 1 ReferenceError: x is not defined
javascript interview question, javascript hoisting interview questions, javascript hoisting tricky questions, hoisting interview questions, hoisting in let and const, javascript coding exercise
Read more…
https://developer.mozilla.org/en-US/docs/Glossary/Hoisting
- JavaScript Coding Interview Question & Answers
- React Interview Question & Answers – Mastering React
- Top React Interview Question & Answer | React Routing
- Top React Interview Questions and Answers | Must Know
- Interview Question React | Basics for Freshers and Seniors
- Cyber Security Fundamental Questions & Answers You must know
- Application & Web Security Interview Questions & Answers
- Angular Interview Questions & Answers 2021 – Part 3 – JS Mount
- Angular Interview Questions & Answers You should know Part 2
- Top 30 JavaScript Interview Questions and Answers for 2021
- NgRx Top Interview Questions and Answers You should know
- Top 40 Awesome CSS Interview Questions & Answers You should know | CSS Tutorial
- Top JavaScript Commonly asked Algorithms in Interview
- Angular NgModule, AOT, and DI, and EntryComponents with example
- TypeScript New & Latest Features You should know | JS Mount
- Rx JS Top Operators with examples | Rx JS interview questions
- What’s new in Angular 9 | Top updates in Angular 9 By JS mount
- What’s new in Angular 8 | Angular 8 Latest Feature By JS mount
- JavaScript Top Interview questions & Answers You should know
- Top 30 TypeScript Interview Questions & Answers You Must Know
- HR Interview Questions and Answers for Experienced
- Mastering JavaScript Interview Questions & Answers
- Commonly Asked Coding Interview Questions JavaScript
- Angular Interview Question & Answers for Experienced – Part 1
- Most Asked JavaScript Interview Questions for Experienced
- Frequently Asked HTML Interview Questions and Answers
- HTML Interview Questions and Answers on Canvas
- JavaScript Console Interview Questions | Input Output Program
- JavaScript Hoisting Interview Questions | Coding Exercise
- JavaScript ++ Operator | X++ and X = X++ Explanation
4 thoughts on “JavaScript Hoisting Interview Questions | Coding Exercise”