🔹 Interview-Based Questions (8 Q&A)
1. What is JavaScript?
Answer:
A scripting language used to create dynamic and interactive behavior in web applications.
2. What are variables in JavaScript?
Answer:
Variables are containers used to store data values.
3. What is the difference between var, let, and const?
Answer:
var → function-scoped, hoisted, can be redeclared
let → block-scoped, cannot be redeclared
const → block-scoped, cannot be reassigned
4. What is hoisting?
Answer:
JavaScript moves variable and function declarations to the top of their scope before
execution.
5. What is block scope?
Answer:
Variables declared inside {} are only accessible within that block.
6. What is use strict?
Answer:
A directive that enables strict mode, enforcing stricter parsing and error handling in
JavaScript.
"use strict";
x = 10; // Error (must declare variable)
7. Can const variables change?
Answer:
The reference cannot change, but object/array contents can be modified.
8. Why is let preferred over var?
Answer:
Because it avoids scope issues and unintended redeclarations.
🔹 Scenario-Based Questions (4 Q&A)
1. Scenario:
A variable is accessible outside a block unexpectedly. Why?
Answer:
It was declared using var (function scope).
2. Scenario:
Code works without declaring variables. What’s wrong?
Answer:
Strict mode is not enabled.
3. Scenario:
You need a variable that should not be reassigned. What will you use?
Answer:
const.
4. Scenario:
You redeclare a variable and get an error. Why?
Answer:
Using let or const (they don’t allow redeclaration in same scope).
🔹 Tricky Interview Questions (4 Q&A)
1. Can var be redeclared?
Answer:
Yes.
2. Is let hoisted?
Answer:
Yes, but not initialized (temporal dead zone).
3. Can const be declared without initialization?
Answer:
No. It must be initialized at declaration.
4. Does use strict improve performance?
Answer:
Not directly; it improves code safety and error detection.
🔹 Example (Important)
"use strict";
let a = 10;
if (true) {
let a = 20;
[Link](a); // 20
}
[Link](a); // 10