Sample questions
Question 1Easy
What is the output?
console.log(x);
var x = 5;
- undefined
- null
- ReferenceError
- 5
Answer: A. undefined
Explanation
The declaration var x is hoisted and initialised to undefined. The assignment of 5 happens only when that line runs.
Step-by-step solution
var declarations are 'hoisted' to the top of their scope during compilation, but only the declaration, not the assignment.- This means
x exists (initialised to undefined) from the very start of the scope, even before the var x = 5; line actually runs. - So
console.log(x), which runs before the assignment line, logs undefined rather than throwing an error.
Why the other options are wrong
- B. null is a distinct, explicit 'empty' value that JavaScript never assigns automatically; hoisted var variables default to undefined, not null.
- C. A ReferenceError would occur only if x were entirely undeclared, but hoisting means the declaration (though not yet the assignment) already exists by this point.
- D. 5 would require the assignment to have already run before this console.log line, but hoisting only moves the declaration up, not the assignment.
Tip: Always declare variables before using them in your code, even though hoisting would technically let you get away with using var ones earlier (as undefined).
Question 2Easy
What is the output?
greet();
function greet() {
console.log("hi");
}
- TypeError
- ReferenceError
- hi
- undefined
Answer: C. hi
Explanation
Function declarations are hoisted together with their body, so they can be called before they appear in the code.
Step-by-step solution
- Function declarations (using the
function name() {} syntax) are hoisted completely, including their entire body, not just their name. - This means
greet is fully defined and callable from the very start of its enclosing scope. - So calling
greet() before its definition appears in the source code still works fine, logging "hi".
Why the other options are wrong
- A. A TypeError would occur if greet were hoisted as some non-function value, but function declarations are hoisted as complete, callable functions.
- B. A ReferenceError would occur only if greet weren't hoisted at all, but function declarations get full hoisting, unlike let/const/class.
- D. undefined would be logged if you tried to console.log(greet) itself in some edge cases before assignment, but here greet() is actually called as a function and does run its body, logging "hi".
Tip: Function declarations can safely be called before they appear in the file; function expressions (assigned to a variable) cannot.
Question 3Easy
What is the output?
console.log(a);
var a = 5;
- 5
- ReferenceError
- undefined
- null
Answer: C. undefined
Explanation
var a is hoisted and initialised to undefined; the assignment happens later.
Step-by-step solution
- Declaration hoisted with undefined.
- console.log runs before a = 5.
Why the other options are wrong
- A. Only the declaration is hoisted, not the assignment.
- B. var declarations are hoisted.
- D. Hoisted vars start as undefined.
Question 4Easy
What is the output?
console.log(add(2, 3));
function add(a, b) { return a + b; }
- ReferenceError
- undefined
- NaN
- 5
Answer: D. 5
Explanation
Function declarations are hoisted with their body, so the call works and returns 5.
Step-by-step solution
- The declaration is hoisted completely.
- add(2, 3) returns 5.
Why the other options are wrong
- A. Function declarations are fully hoisted.
- B. The body is hoisted with the name.
- C. Parameters are bound normally.
Question 5Medium
What is the output?
try {
console.log(sub(5, 2));
} catch (e) {
console.log(e.name);
}
var sub = function (a, b) { return a - b; };
- TypeError
- 3
- ReferenceError
- undefined
Answer: A. TypeError
Explanation
var sub is hoisted as undefined; calling undefined as a function throws TypeError.
Step-by-step solution
- sub is hoisted as undefined.
- Calling undefined throws TypeError.
Why the other options are wrong
- B. Only the var name is hoisted, not the function value.
- C. sub exists as undefined, so it is a TypeError.
- D. Calling undefined throws.
Question 6Medium
What is the output?
function f() { return 'a'; }
var f;
console.log(typeof f);
- function
- undefined
- object
- string
Answer: A. function
Explanation
A repeated var declaration without assignment does not overwrite the hoisted function.
Step-by-step solution
- Function declaration is hoisted.
- var f without a value keeps it.
- typeof gives "function".
Why the other options are wrong
- B. A bare var redeclaration does not reset the function.
- C. Functions report "function".
- D. f is the function, not its return value.
Question 7Medium
What is the output?
try {
console.log(h);
} catch (e) {
console.log(e.name);
}
let h = 1;
- undefined
- 1
- TypeError
- ReferenceError
Answer: D. ReferenceError
Explanation
let bindings are hoisted but stay uninitialised in the temporal dead zone, so reading h early throws ReferenceError.
Step-by-step solution
- h exists but is in the TDZ.
- Reading it throws ReferenceError.
Why the other options are wrong
- A. let is hoisted but not initialised.
- B. The assignment comes after the log.
- C. Access in the TDZ is a ReferenceError.
Question 8Medium
What happens when this code runs?
sayHi();
var sayHi = function () {
console.log("hi");
};
- ReferenceError: sayHi is not defined
- TypeError: sayHi is not a function
- It logs "hi"
- It logs undefined
Answer: B. TypeError: sayHi is not a function
Explanation
Only the var declaration is hoisted, so sayHi is undefined when it is called. Calling undefined throws a TypeError.
Step-by-step solution
- Here,
sayHi is declared with var and assigned a function expression (not a function declaration). - Only the
var declaration is hoisted (as undefined); the assignment of the function to sayHi happens only when that line actually executes. - So when
sayHi() is called before the assignment line runs, sayHi is still undefined, and trying to call undefined as a function throws a TypeError.
Why the other options are wrong
- A. A ReferenceError would occur only if sayHi were completely undeclared, but var hoisting means the name does exist (just as undefined) by the time it's called.
- C. Logging "hi" would require the function expression to have already been assigned before the call, but with var, only the declaration (not the assignment) is hoisted.
- D. Logging undefined would happen if you tried console.log(sayHi), but here sayHi() is actually CALLED as a function, and calling a non-function value throws, rather than just silently returning a value.
Tip: Function expressions (even assigned to var) are NOT safely callable before their assignment line, unlike true function declarations.
Question 9Hard
What is the output?
console.log(typeof f);
if (true) {
function f() {}
}
console.log(typeof f);
- function, function
- undefined, function
- undefined, undefined
- function, undefined
Answer: B. undefined, function
Explanation
In sloppy mode f is var-hoisted as undefined, and the function is assigned once the block executes, so the types are undefined then function.
Step-by-step solution
- f is hoisted as undefined to the top.
- Block executes and f is assigned.
Why the other options are wrong
- A. Block-level function in sloppy mode is hoisted only as undefined first.
- C. The block runs, so f is assigned.
- D. The order is reversed.
Question 10Hard
What is the output?
var a = 1;
function f() {
a = 2;
var a;
}
f();
console.log(a);
- ReferenceError
- 2
- 1
- undefined
Answer: C. 1
Explanation
var a inside f is hoisted to the top of f, so a = 2 assigns the local variable. The global a stays 1.
Step-by-step solution
- Inside function
f, var a; is hoisted to the top of f's own local scope, creating a local a (shadowing the outer global a). - Because of this hoisting, the line
a = 2; inside f assigns to this LOCAL a, not the global one. - The global
a, declared outside f, is never touched by anything inside f, so it remains 1 after f() runs.
Why the other options are wrong
- A. A ReferenceError would occur only if a weren't declared at all inside f, but var a; (even with no assignment) still creates a local declaration that gets hoisted.
- B. 2 would be the result only if a = 2 inside f affected the GLOBAL a, but hoisting of var a inside f creates a separate local variable that shadows the outer one.
- D. undefined would be the value of the local a inside f right after hoisting (before a = 2 runs), but that's irrelevant to what gets logged outside f, which is the untouched global a.
Tip: A variable name declared ANYWHERE in a function with var (even without immediate assignment) is treated as local to that whole function, due to hoisting.
Practise all 13 questions free
The full question bank with timed practice, progress tracking and Vidya, your AI tutor. Free, no ads.
Open this topic in the app →