Question Bank

JavaScript Function Declaration vs Expression Hoisting: Two Explanations Quiz

Difficulty: Medium

Trace mixed declaration / expression calls before either is defined: declaration fully hoisted vs expression-as-var only binding hoisted, plus TDZ for let/const and named function expressions.

Question Bank
/

JavaScript Function Declaration vs Expression Hoisting: Two Explanations Quiz

JavaScript Function Declaration vs Expression Hoisting: Two Explanations Quiz

Trace mixed declaration / expression calls before either is defined: declaration fully hoisted vs expression-as-var only binding hoisted, plus TDZ for let/const and named function expressions.

Question Bank
Medium
JavaScript
4 questions
quiz
js-hoisting
functions
js-language

435 views

7

Explain the exact output of the snippet below and WHY the first line works while the second throws. Use the term "function declaration hoisting" in your answer.

Examples

Example 1:

Input:
console.log(getNum2(5));
console.log(getNum1(3));
var getNum1 = function (n) { return n + 1; };
function getNum2 (n) { return n + 1; }
Output:
6
TypeError: getNum1 is not a function
Explanation: getNum2 is fully hoisted (both name and body); getNum1 is hoisted as a `var` binding initialised to undefined, so calling it throws.