Javascript Functions & Scope Essentials Quiz — Questions & Answers

This quiz contains 15 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: Identifying Function Declaration

    Which of the following is a valid function declaration in JavaScript?

    • function greet() { console.log('Hello'); }
    • let greet = (console.log('Hello'));
    • declare function greet() { console.log('Hello'); }
    • func greet() { console.log('Hello'); }
    • const greet := function() { console.log('Hello'); }
    Show correct answer

    Correct answer: function greet() { console.log('Hello'); }

  2. Question 2: Recognizing Function Expression

    How do you correctly write a function expression assigned to a variable?

    • var hello() = function { return 'Hi'; }
    • let function hello() { return 'Hi'; }
    • var hello = function() { return 'Hi'; }
    • function hello = () { return 'Hi'; }
    • var hello := function() { return 'Hi'; }
    Show correct answer

    Correct answer: var hello = function() { return 'Hi'; }

  3. Question 3: Understanding Arrow Functions

    Which of these correctly defines an arrow function that takes one parameter 'x' and returns 'x*2'?

    • const double = x => x * 2;
    • const double <= x => x * 2;
    • function double = x => x * 2;
    • double: x => x * 2;
    • let double = (x) -> x * 2;
    Show correct answer

    Correct answer: const double = x => x * 2;

  4. Question 4: Default Parameter Values

    How would you specify a default value of 10 for the parameter 'num' in this function: function multiply(num) { ... }?

    • function multiply(10 = num) { ... }
    • function multiply(num == 10) { ... }
    • function multiply(num : 10) { ... }
    • function multiply(num := 10) { ... }
    • function multiply(num = 10) { ... }
    Show correct answer

    Correct answer: function multiply(num = 10) { ... }

  5. Question 5: Function Invocation

    If a function is defined as function sayHello(name) { console.log('Hello ' + name); }, how do you call it for the name "Alex"?

    • sayHello['Alex'];
    • sayHello = 'Alex';
    • call sayHello('Alex');
    • sayHello{'Alex'};
    • sayHello('Alex');
    Show correct answer

    Correct answer: sayHello('Alex');

  6. Question 6: Block Scope with let and var

    What will console.log(output) print in the following code: { let output = 'inside'; } console.log(output);?

    • It throws a ReferenceError
    • It prints [object Object]
    • It prints undefined
    • It prints null
    • It prints 'inside'
    Show correct answer

    Correct answer: It throws a ReferenceError

  7. Question 7: Function Hoisting

    What is the result of calling a function declared as function greet() { return 'Hi'; } before its definition in the code?

    • It causes an undefined error
    • It works because the function is hoisted
    • It prints the function code
    • It returns null
    • It raises a SyntaxError
    Show correct answer

    Correct answer: It works because the function is hoisted

  8. Question 8: Anonymous Function Use

    Which example shows an anonymous function being passed as an argument to another function?

    • setTimeout(function() { alert('Hi'); }, 1000);
    • callTimeout(setTimeout(), 1000);
    • function myFunc() { }
    • const f = () => {};
    • let test = function named() { };
    Show correct answer

    Correct answer: setTimeout(function() { alert('Hi'); }, 1000);

  9. Question 9: Closures Simplified

    What is a closure in JavaScript?

    • A function that returns itself
    • A property that closes objects
    • A function that remembers the environment in which it was created
    • A function that runs at the end of a script
    • A method that automatically executes
    Show correct answer

    Correct answer: A function that remembers the environment in which it was created

  10. Question 10: Recursion Recognition

    What term describes a function that calls itself directly or indirectly in JavaScript?

    • Recursive function
    • Repeatable function
    • Retrospective function
    • Iterative function
    • Circular function
    Show correct answer

    Correct answer: Recursive function

  11. Question 11: Arrow Function Return Value

    Which arrow function correctly returns the sum of 'a' and 'b'?

    • const add = (a, b) => { a + b };
    • const add = a, b => return a + b;
    • const add = (a, b) <- a + b;
    • const add = (a, b) => a + b;
    • const add = (a, b) < a + b >;
    Show correct answer

    Correct answer: const add = (a, b) => a + b;

  12. Question 12: Global vs Local Scope

    If you declare a variable with 'var' inside a function, where is it accessible?

    • Only inside that function
    • Everywhere in the code
    • Only within the block after declaration
    • As a global constant
    • In the entire file except imported modules
    Show correct answer

    Correct answer: Only inside that function

  13. Question 13: Parameter vs Argument

    What is the correct term for the value passed to a function when calling it?

    • Argument
    • Declaration
    • Statement
    • Parameter
    • Constructor
    Show correct answer

    Correct answer: Argument

  14. Question 14: Function Return Statement

    What will the following code print: function test() { return 5; } console.log(test());

    • Error
    • undefined
    • 5
    • test
    • null
    Show correct answer

    Correct answer: 5

  15. Question 15: Default Parameters Omitted

    What will the following output: function greet(name = 'Friend') { console.log('Hi ' + name); } greet();

    • Hi Friend
    • undefined
    • Error
    • Hi
    • Friend
    Show correct answer

    Correct answer: Hi Friend