🧠 **What will be the output of the following JavaScript code?** ### 🔹 Snippet 1 ```js let x = 1; { let x = 2; } console.log(x); ``` ### 🔹 Snippet 2 ```js var x = 1; { var x = 2; } console.log(x); ``` --- ### ✅ Correct Answer: 👉 **1, 2** --- ### 💡 Why? 🔵 **`let` is block-scoped** In Snippet 1: * `let x = 2` exists only inside the block `{ }` * It does NOT affect the outer `x` * So `console.log(x)` prints → **1** --- 🟠 **`var` is function-scoped** In Snippet 2: * `var x = 2` overrides the same variable * `var` ignores block scope * So `console.log(x)` prints → **2** --- ### 🚀 Key Takeaway: ✔ `let` → Block scoped ✔ `var` → Function scoped ✔ Always prefer `let` and `const` in modern JavaScript --- 🎯 This is one of the most common JavaScript interview traps. Would you have answered it correctly? 👇 #JavaScript #FrontendDevelopment #WebDevelopment #CodingInterview #JSConcepts #Programming #SoftwareEngineering #DeveloperLife #LearnToCode #TechCommunity #FullStackDeveloper #100DaysOfCode
1,2
1,2
1,2
1,2
1,2
1,1
1,1
1,2
1,1
1,2 Explanation: let is block scoped so when you console let outside block then it doesn’t know about the inner one so 1 var only have function scope so when you change inside block it does reflect outside and the console will print as 2