Yogesh Chavan
1 min readJul 25, 2020

--

DE Thanks! var related questions are asked in most of the JavaScript interviews to check If the candidate knows when to use var and when to use let / const because var is a function scoped variable and not block scoped so the following code is valid in case of var

for(var i = 0; i < 5; i++){
// do something
}

console.log(i); // i will be 5 here and is still accessible outside the for loop as it's not block scoped variable when we use var keyword

But if we use let variable like this

for(let i = 0; i < 5; i++){
// do something
}

console.log(i); // this will generate error because variable i declared using let makes it a block scoped variable so it's not accessible outside the for loop.

Hoisting related questions are also asked in some interviews because they are related to var keyword.

--

--

Yogesh Chavan
Yogesh Chavan

No responses yet