Javascript — Tricky Questions- Hoisting

Eishta Mittal
4 min readMay 23, 2022

--

Q.1 What will be the output of the below code?

console.log(a);
a = 1; // a is not declared anywhere

Answer: ReferenceError: a is not defined

Q.2 What will be the output of the below code?

console.log(b);
var b = 1 ;

Answer: undefined

Q.3 What will be the output of the below code?

console.log('bar:', bar); 
bar = 15;
var foo = 1;
console.log(foo, bar);
var bar;

Answer:
bar: undefined
1 15

Q.4 What will be the output of the below code?

 var foo = 5
console.log(‘foo:’, foo)
var foo;
var bar = 10;
var bar;
console.log(‘bar:’, bar)
var baz = 10
var baz = 12
console.log(‘baz:’, baz)

Answer:
foo: 5
bar: 10
baz: 12

Q.5 What will be the output of the below code?

var a = 1;
console.log(a);
var a = 2;
console.log(a);

Answer:
1
2

Q.6 What will be the output of the below code?

function foo() {
function bar() { // overridden during compilation
return 5
}
return bar()
function bar() {
return 10
}
}
console.log(foo());

Answer: 10

Q.7 What will be the output of the below code?

function printA() {  
console.log("Value of a after declaration", a);
}
console.log("Value of a before declaration: ", a);
var a = 1;
printA(); // NOTE: called after a is declared and initialised

Answer:
Value of a before declaration: undefined
Value of a after declaration 1

Q.8 What will be the output of the below code?

function printA() { 
a = 1; // a gets space in global scope
}
printA();
console.log(a); // a is accessible

Answer: 1

Q.9 What will be the output of the below code?

function printA() { 
var a = 1; // a is enclosed in function scope
}
printA();
console.log(a); // a is not accessible

Answer: ReferenceError: a is not defined

Q.10 What will be the output of the below code?

function a() {
var b = function () {
return 3;
};
return b();
var b = function () {
return 8;
};
}
alert(a());

Answer: 3
Why ? => b is function expression and is assigned the function during execution phase , before assigning the second value to b, the function a returns b()

Q.11 What will be the output of the below code?

function a() {
function b() {
return 3;
}
return b();
function b() {
return 8;
}
}
alert(a());

Answer: 8
Why? => b is function declaration and is assigned the definition during compilation phase and second definition overrides the first one

Q.12 Preference given if a function and variable has same name.

function foo() {
var bar = "I'm a bar variable". // 2nd hoisting
function bar() { // function is hoisted first
return "I'm a bar function"
}
return bar()
}
console.log(foo())

Answer: TypeError: bar is not a function
Because the function dec. is hoisted first and then overridden by the var i.e. now b() gives the type error

Q.13 Precedence of function expressions and function declaration

greeting()
var greeting = function () {
console.log('Good morning')
}
greeting()
function greeting() {
console.log('Good evening')
}
greeting()

Answer: Good evening
Good morning
Good morning

Q.14 What will be the output of the below code?

var x = 'foo';
(function() {
console.log('x: ' + x)
var x = 'bar'
console.log('x: ' + x)
})()

Answer:
x: undefined
x: bar

Q.15 What will be the output of the below code?

console.log('bar:', bar);
bar = 15;
var foo = 1;
console.log("foo:", foo, "bar:", bar);
var bar;

Answer:
bar: undefined
foo: 1 bar: 15

Q.16 What will be the output of the below code?

var v1 = 10;
(function(){
console.log(v1); // 10
v1 = 20; // it is not a declaration, declaration is hoisted
console.log(v1); // 20

})();
console.log(v1); // 20
var v1 = 30;

Answer: 10, 20, 20
All the references to v1 are to the same variable as, in the function , v1 is not declared so its scope is not limited to the function.

it would have been different if :

var v1 = 10;
(function(){
console.log(v1); // undefined
var v1 = 20;
console.log(v1); // 20

})();
console.log(v1); // 10
var v1 = 30;

Answer: undefined, 20 , 10
As v1 is declared inside the function, it will be limited to this function and is hoisted inside the function

Q.17 What will be the output of the below code?

var v1 = 10;
(function(){
v3 = 35 // hoisted and assigned a value
console.log(v3) // 35
var v3 = 45; // declaration in function
v2 = 25; // global variable
console.log(v1); // 10

})();
console.log(v2); // 25
console.log(v3); // ReferenceError: v3 is not defined
var v1 = 30;

Answer:
35, 10, 25, Ref Error

Q.18 What will be the output of the below code?

(function () {
try {
throw new Error();
} catch (x) {
var x = 1, y = 2;
console.log(x);
}
console.log(x);
console.log(y);
})();

This code after hoisting looks like :-

(function () {
var x, y; // outer and hoisted
try {
throw new Error();
} catch (x /* inner */) {
x = 1; // inner x, not the outer one
y = 2; // there is only one y, which is in the outer scope
console.log(x /* inner */);
}
console.log(x);
console.log(y);
})();

Answer:
1
undefined
2

--

--