These questions cover the most common let, const and Temporal Dead Zone patterns asked in JavaScript interviews. Start with basic cases and then move towards shadowing, block scope and tricky TDZ examples.
Question 1
console.log(a); let a = 10;
The variable exists but is inside the Temporal Dead Zone.
Question 2
console.log(a); const a = 20;
const also stays inside TDZ before declaration.
Question 3
let a = 10; console.log(a);
Access happens after declaration.
Question 4
const a = 50; console.log(a);
const works normally after initialization.
Question 5
let a = 10; let a = 20;
let cannot be redeclared in the same scope.
Question 6
const a = 10; a = 20;
A const variable cannot be reassigned.
Question 7
let a = 10; a = 30; console.log(a);
let allows reassignment.
Question 8
{
let a = 100;
}
console.log(a);
let is block scoped.
Question 9
{
const a = 200;
}
console.log(a);
const is also block scoped.
Question 10
let a = 10;
{
let a = 20;
console.log(a);
}
console.log(a);
The inner variable shadows the outer variable.
Question 11
let a = 10;
{
console.log(a);
let a = 20;
}
The inner variable is inside TDZ.
Question 12
var a = 10;
{
let a = 20;
}
console.log(a);
The block-scoped variable does not affect the global variable.
Question 13
const obj = {
name: "John"
};
obj.name = "Peter";
console.log(obj.name);
Object properties can still be changed.
Question 14
const arr = [1,2]; arr.push(3); console.log(arr.length);
Array contents can be modified.
Question 15
const obj = {};
obj = {name:"John"};
The reference itself cannot change.
Question 16
let a; console.log(a);
The variable is declared but not assigned.
Question 17
const a;
const must be initialized during declaration.
Question 18
typeof a; let a = 10;
typeof does not bypass TDZ.
Question 19
let a = 10;
function test(){
console.log(a);
}
test();
Functions use lexical scope.
Question 20
let a = 10;
{
let b = 20;
{
console.log(a + b);
}
}
Nested blocks can access variables from outer blocks.
Question 21
for (let i = 0; i < 3; i++) {
setTimeout(() => console.log(i), 0);
}
Each iteration gets its own block-scoped variable.
Question 22
for (var i = 0; i < 3; i++) {
setTimeout(() => console.log(i), 0);
}
var shares one variable across all iterations.
Question 23
let a = 10;
{
let a = a + 1;
}
The inner variable is in TDZ while being initialized.
Question 24
let a = 5;
{
console.log(a);
let b = 10;
}
Outer variables remain accessible.
Question 25
{
let a = 10;
}
{
let a = 20;
}
console.log("done");
Different blocks can use the same variable name.
Question 26
let a = 10;
function test() {
let a = 20;
console.log(a);
}
test();
The local variable shadows the outer variable.
Question 27
let a = 10;
{
console.log(a);
{
let a = 20;
}
}
The inner declaration does not affect the outer block.
Question 28
let a = 10;
{
let a = 20;
{
console.log(a);
}
}
The nearest scope is used.
Question 29
const user = {
name: "John"
};
delete user.name;
console.log(user.name);
Properties can be removed from a const object.
Question 30
const arr = [1,2]; arr[0] = 100; console.log(arr[0]);
Array elements remain mutable.
Question 31
function test(a = b, b = 2) {
return a + b;
}
test();
b is in TDZ when a's default value is evaluated.
Question 32
function test(a = 1, b = a) {
return b;
}
console.log(test());
Earlier parameters are available to later parameters.
Question 33
switch (1) {
case 1:
let a = 10;
console.log(a);
break;
}
The declaration belongs to the switch block.
Question 34
let a = 10;
function test() {
console.log(a);
}
{
let a = 20;
test();
}
Functions use lexical scope, not calling scope.
Question 35
let a = 10;
{
console.log(typeof a);
let b = 20;
}
a is already initialized in the outer scope.
Question 36
{
console.log(typeof a);
let a = 10;
}
typeof does not avoid TDZ errors.
Question 37
let a = 10;
if (true) {
const a = 20;
console.log(a);
}
console.log(a);
Block scope creates a separate variable.
Question 38
const a = {
value: 10
};
a.value++;
console.log(a.value);
Object properties can still change.
Question 39
let a = 10;
{
let b = 20;
}
console.log(typeof b);
typeof on an undeclared variable returns "undefined".
Question 40
let a = 10;
{
let a = 20;
{
let a = 30;
console.log(a);
}
console.log(a);
}
console.log(a);
Each block creates its own scope and shadows outer variables.