JavaScript Functions and Scope Deep Dive Quiz — Questions & Answers

Challenge your understanding of JavaScript functions, scoping rules, closures, recursion, and related advanced concepts. Ideal for those looking to test or refine their mastery of these fundamental topics.

This quiz contains 16 questions. Below is a complete reference of all questions, answer choices, and correct answers. You can use this section to review after taking the interactive quiz above.

  1. Question 1: Function Declaration Hoisting

    What is the output of the following code? console.log(add(2, 3)); function add(a, b) { return a + b; }

    • TypeError
    • 5
    • undefined
    • null
    • ReferenceError
    Show correct answer

    Correct answer: 5

  2. Question 2: Function Expression Hoisting

    Given the code below, what happens when calling multiply(2, 4) before its definition? console.log(multiply(2, 4)); var multiply = function(a, b) { return a * b; };

    • undefined
    • TypeError
    • ReferenceError
    • 8
    • null
    Show correct answer

    Correct answer: TypeError

  3. Question 3: Arrow Function Lexical 'this'

    In the context of arrow functions, how is the value of 'this' determined when called inside an object method?

    • 'this' points to the object the method belongs to
    • 'this' is taken from the enclosing lexical scope
    • 'this' refers to the global object always
    • 'this' is always window
    • 'this' is undefined in strict mode
    Show correct answer

    Correct answer: 'this' is taken from the enclosing lexical scope

  4. Question 4: Default Parameter Evaluation

    What value is assigned to y in function call myFunc(undefined) given this definition? function myFunc(y = 7) { return y; }

    • NaN
    • 0
    • null
    • 7
    • undefined
    Show correct answer

    Correct answer: 7

  5. Question 5: Rest Parameters Syntax

    Which syntax correctly allows a function to accept any number of arguments as an array named items?

    • function collect(*items) {}
    • function collect(items[]) {}
    • function collect(...items) {}
    • function collect([...items]) {}
    • function collect(items...) {}
    Show correct answer

    Correct answer: function collect(...items) {}

  6. Question 6: Block Scoping with let and var

    What is logged after executing the following? { let x = 10; } console.log(typeof x);

    • "object"
    • "number"
    • "undefined"
    • "boolean"
    • "string"
    Show correct answer

    Correct answer: "undefined"

  7. Question 7: Closures Capturing Variables

    Which output will result from this code? function outer() { let counter = 0; return function() { counter++; return counter; }; } const inc = outer(); inc(); inc(); console.log(inc());

    • 0
    • 2
    • 3
    • NaN
    • 1
    Show correct answer

    Correct answer: 3

  8. Question 8: Recursive Function Return Value

    Given this recursive factorial implementation, what does factorial(3) return? function factorial(n) { if (n === 0) return 1; else return n * factorial(n - 1); }

    • 1
    • 9
    • 3
    • 6
    • 0
    Show correct answer

    Correct answer: 6

  9. Question 9: Function Name Binding

    What is the value of typeof bar inside the following function expression? const bar = function baz() { return typeof baz; };

    • "string"
    • "undefined"
    • ReferenceError
    • "function"
    • "object"
    Show correct answer

    Correct answer: "function"

  10. Question 10: Shadowed Variables Scope

    What will the following code log to the console? let y = 5; (function() { let y = 10; console.log(y); })(); console.log(y);

    • Error
    • 5 then 5
    • 10 then 5
    • 5 then 10
    • 10 then 10
    Show correct answer

    Correct answer: 10 then 5

  11. Question 11: Parameter Assignment Behavior

    What is logged by this code? function foo(a) { a = 4; } let b = 3; foo(b); console.log(b);

    • undefined
    • ReferenceError
    • 4
    • null
    • 3
    Show correct answer

    Correct answer: 3

  12. Question 12: Immediately Invoked Function Expression (IIFE)

    Which pattern immediately executes a function upon definition?

    • function execute(){ /* code */ };
    • (function(){ /* code */ })();
    • (() => {/* code */})
    • function(){ /* code */ }();
    • function() IIFE {}
    Show correct answer

    Correct answer: (function(){ /* code */ })();

  13. Question 13: Arguments Object in Arrow Functions

    What is the value of arguments[0] inside an arrow function?

    • null
    • 'undefined'
    • ReferenceError
    • NaN
    • The actual first argument
    Show correct answer

    Correct answer: ReferenceError

  14. Question 14: Recursive Call Stack Overflow

    What happens when a recursive function lacks an explicit base case in JavaScript?

    • It eventually throws a RangeError due to stack overflow
    • It returns null automatically
    • It halts with a SyntaxError
    • It returns undefined by default
    • It repeatedly returns 0
    Show correct answer

    Correct answer: It eventually throws a RangeError due to stack overflow

  15. Question 15: Default Parameters and Undefined

    Given function test(a = 2) { return a; }, what is the output of test()?

    • 2
    • undefined
    • ReferenceError
    • null
    • 0
    Show correct answer

    Correct answer: 2

  16. Question 16: Object Methods and Arrow Functions

    If obj = {val: 2, getVal: () => this.val}, what is the result of calling obj.getVal()?

    • 2
    • NaN
    • undefined
    • ReferenceError
    • null
    Show correct answer

    Correct answer: undefined