JavaScript interviews love questions that test scope, hoisting, and execution context. Take a look at this snippet and ask yourself: 👉 What will be logged to the console, and why? This question revolves around: • var hoisting behavior • Function scope vs global scope • Shadowing of variables • Execution order in JavaScript It’s a great example of why understanding how JavaScript works internally matters more than just writing code that “works”. Drop your answer in the comments before running it 👇 Let’s test our fundamentals. #JavaScript #JSInterview #Hoisting #Scope #FrontendDevelopment #InterviewPreparation #WebDevelopment
What's new here
undefined undefined 20 10
Undefined Undefined 20 10 1) First answer is Undefined due to variable hoisting 2) Second answer is Undefined because we can't access global variable inside the local scope. Inside the function prints the variable before declaration that's why output is undefined. 3) Third answer is 20 because it is declared inside the local scope that's why it can be accessable inside the local scope. Variable is declared first then it prints that's why output is 20. 4) Last answer is 10 because it is global scope that's why we can access from outside the function or local scope But if we use let/const instead of var then we will get reference error due to TDZ
Undefined (log before declaration) Undefined (log before declaration ) 20 (in the function after declaration) 10 (global output ) Javascript allow us to use function and variable before they are declared In this case we are using var so we didn't get any error but when we use let and const they stay in temporal dead zone and we will face an error like reference error Concept : javascript Hoisting
undefined undefined 20 10 Concept hoisting, scope and veriable shadowing
Anis Rahman Sadly, this question ignores some of the most basic JavaScript programming advice that's been around for years (var vs let/const and variable shadowing). IMO You wouldn't be testing the interviewees knowledge/skills, you'd be asking him to perform a trick.
undefined undefined 20 10
The Output will be Undefined Undefined 20 10 Because of Hoisting
Anis Rahman, if recruiters ask this question, they do NOT know programming‼️