Scope decides where a variable can be accessed. Scope chain decides how JavaScript looks for a variable when it is not available in the current scope. These questions cover the most common interview patterns around global scope, function scope, block scope, nested scope and variable lookup.
Question 1
let name = "John";
function test() {
console.log(name);
}
test();
The function can access variables from the global scope.
Question 2
function test() {
let age = 25;
}
console.log(age);
Variables declared inside a function are not available outside.
Question 3
let a = 10;
function test() {
let b = 20;
console.log(a);
console.log(b);
}
test();
The function can access both local and global variables.
Question 4
let a = 10;
function outer() {
let b = 20;
function inner() {
console.log(a);
console.log(b);
}
inner();
}
outer();
The inner function can access variables from all outer scopes.
Question 5
let a = 10;
function test() {
let a = 20;
console.log(a);
}
test();
The local variable shadows the global variable.
Question 6
let a = 10;
function test() {
console.log(a);
}
test();
JavaScript finds the variable in the outer scope.
Question 7
function test() {
console.log(a);
}
test();
JavaScript cannot find the variable in any scope.
Question 8
let city = "Delhi";
function outer() {
let state = "UP";
function inner() {
console.log(city);
console.log(state);
}
inner();
}
outer();
The scope chain includes all parent scopes.
Question 9
let a = 10;
function outer() {
let a = 20;
function inner() {
console.log(a);
}
inner();
}
outer();
The nearest variable in the scope chain is used.
Question 10
let a = 10;
function outer() {
function inner() {
console.log(a);
}
inner();
}
outer();
The variable is found in the global scope.
Question 11
let a = 10;
{
let b = 20;
console.log(a);
}
Inner blocks can access outer variables.
Question 12
{
let b = 20;
}
console.log(b);
Block-scoped variables are not accessible outside the block.
Question 13
let a = 1;
function first() {
function second() {
function third() {
console.log(a);
}
third();
}
second();
}
first();
The scope chain continues through multiple levels.
Question 14
let a = 100;
function outer() {
let a = 200;
function inner() {
let a = 300;
console.log(a);
}
inner();
}
outer();
The closest variable is always selected first.
Question 15
let a = 100;
function outer() {
let b = 200;
function inner() {
console.log(b);
}
inner();
}
outer();
The variable is found in the immediate parent scope.
Question 16
let a = 10;
function test() {
let b = 20;
console.log(a + b);
}
test();
Variables from different scopes can be used together.
Question 17
let value = 5;
function test() {
let value = 10;
console.log(value);
}
test();
console.log(value);
Local and global variables are separate.
Question 18
let a = 10;
function outer() {
function inner() {
console.log(a);
}
let a = 20;
inner();
}
outer();
The function uses the scope where it was created.
Question 19
let a = 10;
function outer() {
let b = 20;
function inner() {
let c = 30;
console.log(a + b + c);
}
inner();
}
outer();
The innermost function can access every parent scope.
Question 20
let a = 1;
function outer() {
let a = 2;
function inner() {
let a = 3;
console.log(a);
}
inner();
}
outer();
The nearest variable in the scope chain wins.
Question 21
let a = 10;
function test() {
console.log(a);
if (true) {
let a = 20;
}
}
test();
The inner block variable does not affect the outer scope.
Question 22
let a = 10;
function test() {
if (true) {
let a = 20;
console.log(a);
}
console.log(a);
}
test();
Block scope and outer scope are different variables.
Question 23
let a = 1;
function outer() {
let b = 2;
function inner() {
let c = 3;
console.log(a);
console.log(b);
console.log(c);
}
inner();
}
outer();
The innermost function can access all parent scopes.
Question 24
function outer() {
let b = 2;
function inner() {
console.log(b);
}
return inner;
}
const fn = outer();
fn();
The returned function keeps access to its lexical scope.
Question 25
let a = 10;
function first() {
function second() {
console.log(a);
}
return second;
}
const fn = first();
fn();
The function remembers where it was created.
Question 26
let a = 10;
function test() {
console.log(a);
let a = 20;
}
test();
The local variable exists but is inside TDZ.
Question 27
let a = 10;
function outer() {
let a = 20;
function inner() {
console.log(a);
}
return inner;
}
const fn = outer();
fn();
The returned function closes over the nearest variable.
Question 28
let a = 1;
function outer() {
let a = 2;
function inner() {
let a = 3;
console.log(a);
}
return inner;
}
outer()();
The closest variable is selected.
Question 29
let a = 100;
function test() {
console.log(a);
}
function execute() {
let a = 200;
test();
}
execute();
JavaScript uses lexical scope, not calling scope.
Question 30
let a = 10;
{
let b = 20;
{
let c = 30;
console.log(a + b + c);
}
}
Nested scopes can access all outer variables.
Question 31
let a = 1;
{
let a = 2;
{
let a = 3;
console.log(a);
}
console.log(a);
}
console.log(a);
Every block creates a separate scope.
Question 32
function test() {
let value = 100;
return function() {
console.log(value);
};
}
test()();
The returned function retains access to the parent scope.
Question 33
let a = 10;
function outer() {
let b = 20;
function inner() {
let c = 30;
return a + b + c;
}
return inner();
}
console.log(outer());
The scope chain reaches all parent levels.
Question 34
let name = "John";
function test() {
let name = "Peter";
console.log(name);
}
test();
console.log(name);
Local variables do not change global variables.
Question 35
let a = 10;
function outer() {
function inner() {
console.log(a);
}
let a = 20;
return inner;
}
outer()();
The function closes over outer's variable, not the global one.
Question 36
let count = 1;
function first() {
let count = 2;
function second() {
let count = 3;
console.log(count);
}
second();
}
first();
The nearest variable always wins.
Question 37
let a = 10;
function test() {
console.log(typeof a);
}
test();
The variable is found in the global scope.
Question 38
{
let a = 10;
}
typeof a;
Outside the block, a is not declared in the current scope.
Question 39
let a = 10;
function outer() {
let b = 20;
function inner() {
console.log(b);
}
return inner;
}
const fn = outer();
fn();
The closure keeps access to b even after outer finishes.
Question 40
let a = 1;
function outer() {
let b = 2;
function middle() {
let c = 3;
function inner() {
console.log(a + b + c);
}
inner();
}
middle();
}
outer();
The scope chain can travel through multiple parent functions.