J

JavaScript Handbook

Clean • Professional

JavaScript Code Blocks & Scope — Top Interview Q&A

4 minute

JavaScript Code Blocks & Scope — Interview Questions & Answers

Ques: What is a code block in JavaScript?

Ans: A code block in JavaScript is a group of statements enclosed in curly braces {}. It is mainly used to define the boundaries of control structures like if, for, and while, and to create block-level scopes.

Example:

{
  let message = "Inside Block";
  console.log(message); // Inside Block
}
console.log(message); //  Error

Ques: What is meant by scope in JavaScript?

Ans: Scope defines where a variable can be accessed or modified within the code. It ensures that variables are used only in their intended areas.

There are 3 main types of scope:

  1. Global Scope
  2. Function Scope
  3. Block Scope (introduced in ES6)

Ques: What is the difference between Global, Function, and Block Scope?

Scope TypeDeclared WithAccessible WhereExample
GlobalAnywhere outside functionsAnywhere in codevar name = "JS";
FunctionInside a functionOnly inside that functionfunction test(){ var x=5; }
BlockInside {} using let or constOnly inside that block{ let y=10; }

Ques: What is block scope? Give an example.

Ans: Variables declared with let or const are block-scoped, meaning they exist only inside the block {} where they’re defined.

Example:

{
  let age = 25;
  console.log(age); // 25
}
console.log(age); //  Error: age is not defined

Ques: What is the difference between var, let, and const in terms of scope?

KeywordScope TypeRedeclarationReassignmentHoisted
varFunction ScopedYesYesYes (undefined)
letBlock ScopedNoYesYes (Temporal Dead Zone)
constBlock ScopedNoNoYes (TDZ)

Ques: What is the Temporal Dead Zone (TDZ)?

Ans: TDZ is the time between variable hoisting and initialization, where accessing a variable before it’s declared using let or const causes an error.

Example:

console.log(a); //  ReferenceError
let a = 10;

Ques: What happens if you declare a variable using var inside a block?

Ans: var is function-scoped, not block-scoped**, so it’s still accessible outside the block.

Example:

if (true) {
  var color = "blue";
}
console.log(color); //  "blue"

Ques: Can we create nested blocks in JavaScript?

Ans: Yes, nested blocks are allowed. Each block creates its own scope layer for let and const.

Example:

{
  let a = 10;
  {
    let b = 20;
    console.log(a, b); // 10 20
  }
  console.log(a); // 10
  console.log(b); //  Error
}

Ques: What is variable shadowing?

Ans: Shadowing occurs when an inner variable has the same name as an outer variable. The inner variable temporarily hides the outer one within its block.

Example:

let count = 5;
{
  let count = 10;
  console.log(count); // 10
}
console.log(count); // 5

Ques: What is illegal shadowing in JavaScript?

Ans: When you try to shadow a let or const variable with a var in the same or inner scope — it causes an error.

Example:

let x = 10;
{
  var x = 20; //  SyntaxError: Identifier 'x' has already been declared
}

Legal shadowing:

var x = 10;
{
  let x = 20; //  Works fine
}

Ques: What is variable leakage in JavaScript?

Ans: Variable leakage happens when a variable declared inside a block using var is accidentally available outside the block.

Example:

for (var i = 0; i < 3; i++) {}
console.log(i); // 3 (leaked)

Fixed:

for (let i = 0; i < 3; i++) {}
console.log(i); //  Error

Ques: Can functions create their own scope blocks?

Ans: Yes, functions always create a new scope for variables declared inside them. Variables inside a function are not accessible outside it.

Example:

function greet() {
  let name = "John";
  console.log(name);
}
greet();
console.log(name); //  Error

Ques: How does scope chaining work?

Ans: When a variable is used, JavaScript searches through scopes upward — from the current scope → parent → global — until it finds the variable.

Example:

let a = 1;
function outer() {
  let b = 2;
  function inner() {
    let c = 3;
    console.log(a, b, c); // 1, 2, 3
  }
  inner();
}
outer();

Ques: How do code blocks affect memory usage and performance?

Ans: Each block scope creates temporary variables that are garbage collected after the block executes — helping improve memory management. Keeping variables block-scoped (using let or const) ensures faster lookups and less global pollution.

Article 0 of 0