Question 1
var a = 1;
function test() {
console.log(a);
var a = 2;
}
test();
The local var declaration is hoisted inside the function and shadows the global variable.
Question 2
foo();
function foo() {
console.log("A");
}
function foo() {
console.log("B");
}
When multiple function declarations exist, the later declaration wins during hoisting.
Question 3
var a = 10;
function a() {}
console.log(typeof a);
The function is hoisted first, but the later assignment stores 10 in a.
Question 4
console.log(foo);
function foo() {}
foo = 100;
console.log(typeof foo);
The function exists initially, then gets replaced by a number.
Question 5
function test() {
console.log(foo);
function foo() {}
}
test();
Function declarations are fully available inside their scope before execution.
Question 6
var foo = function() {
return "A";
};
console.log(foo());
foo = function() {
return "B";
};
console.log(foo());
Function expressions behave like normal variable assignments.
Question 7
console.log(typeof foo);
var foo = function() {};
Only the variable declaration is hoisted.
Question 8
var a = 1;
function test() {
console.log(a);
a = 5;
console.log(a);
}
test();
console.log(a);
No local declaration exists, so the global variable is updated.
Question 9
var a = 1;
function test() {
a = 5;
var a;
console.log(a);
}
test();
console.log(a);
The local var is hoisted, so a=5 affects the local variable, not the global one.
Question 10
function test() {
return foo();
function foo() {
return "JS";
}
}
console.log(test());
The function declaration is available throughout the scope.
Question 11
console.log(foo);
var foo;
function foo() {}
Function declarations take precedence over var declarations.
Question 12
function foo() {
console.log("A");
}
var foo;
foo();
The var declaration does not overwrite the function.
Question 13
var foo = 1;
function test() {
console.log(foo);
function foo() {}
}
test();
The local function declaration shadows the global variable.
Question 14
function test() {
console.log(typeof foo);
var foo = 100;
}
test();
The local variable exists as undefined before assignment.
Question 15
function foo() {
return 1;
}
function foo() {
return 2;
}
console.log(foo());
The last function declaration overrides earlier ones during hoisting.
Question 16
let a = 10;
{
console.log(a);
let a = 20;
}
The inner variable creates a Temporal Dead Zone for the entire block.
Question 17
let a = 1;
{
let a = a + 1;
}
The inner a is still in its TDZ while the initializer is evaluated.
Question 18
const a = 10;
{
console.log(a);
const b = 20;
}
The block can access variables from outer scopes.
Question 19
{
console.log(typeof x);
let x = 5;
}
typeof does not bypass the TDZ.
Question 20
let x = 10;
function test() {
console.log(x);
let x = 20;
}
test();
The local x shadows the outer x and remains in TDZ.
Question 21
const user = {
name: "John"
};
user.name = "Peter";
console.log(user.name);
const prevents reassignment, not object mutation.
Question 22
const arr = [1]; arr.push(2); console.log(arr.length);
The array contents can still be modified.
Question 23
let a = 10;
{
console.log(a);
{
let a = 20;
}
}
The inner declaration belongs to a different block and does not affect the outer lookup.
Question 24
{
let a = 10;
{
console.log(a);
}
}
Nested blocks can access parent block variables.
Question 25
let a = 10;
{
let b = 20;
{
console.log(a + b);
}
}
Scope resolution travels outward through nested blocks.
Question 26
{
function foo() {
return "JS";
}
console.log(foo());
}
The function is available within the block where it is declared.
Question 27
let a = 5;
function test(a) {
console.log(a);
}
test(10);
Function parameters create local bindings that shadow outer variables.
Question 28
function test(a) {
console.log(a);
let b = 20;
}
test();
Missing arguments receive undefined.
Question 29
let x = 1;
function test() {
{
console.log(x);
}
let x = 2;
}
test();
The function-level x is in TDZ throughout the block before initialization.
Question 30
let a = 1;
function test() {
return function inner() {
return a;
};
}
console.log(test()());
The returned function resolves variables through its lexical environment.
Question 31
function first() {
console.log("A");
}
function second() {
console.log("B");
first();
console.log("C");
}
second();
Execution enters second(), pauses while first() runs, then resumes.
Question 32
function one() {
console.log("1");
}
function two() {
one();
console.log("2");
}
function three() {
two();
console.log("3");
}
three();
The call stack becomes three → two → one and then unwinds.
Question 33
function test() {
console.log("A");
return "B";
}
console.log(test());
The function executes first and its return value is then logged.
Question 34
function test() {
console.log("A");
return;
console.log("B");
}
test();
Execution stops immediately after return.
Question 35
function sum(a, b) {
return a + b;
}
function display() {
return sum(2, 3);
}
console.log(display());
The return value travels back through the call stack.
Question 36
function count(n) {
if (n === 0) {
return;
}
console.log(n);
count(n - 1);
}
count(3);
The log occurs before the recursive call.
Question 37
function count(n) {
if (n === 0) {
return;
}
count(n - 1);
console.log(n);
}
count(3);
The logs occur while recursive calls are removed from the stack.
Question 38
function outer() {
console.log("A");
function inner() {
console.log("B");
}
inner();
console.log("C");
}
outer();
inner() completes before execution continues.
Question 39
function first() {
return second();
}
function second() {
return "JS";
}
console.log(first());
The returned value moves back through all active execution contexts.
Question 40
function test() {
console.log("A");
return "B";
}
const result = test();
console.log(result);
The function executes first, then the stored result is logged.
Question 41
function multiply(a, b) {
return a * b;
}
console.log(
multiply(
multiply(2, 2),
3
)
);
The inner function call completes before the outer call can execute.
Question 42
function test() {
return;
}
console.log(test());
A function without a return value returns undefined.
Question 43
function a() {
console.log("A");
}
function b() {
console.log("B");
a();
}
b();
a();
After b() completes, the separate a() call executes.
Question 44
function test(n) {
if (n === 1) {
return 1;
}
return n + test(n - 1);
}
console.log(test(3));
The stack evaluates:
3 + test(2)
2 + test(1)
1
Then unwinds to produce 6.
Question 45
function first() {
console.log("1");
second();
console.log("2");
}
function second() {
console.log("3");
}
first();
Execution pauses at second(), then resumes after it finishes.