Higher-Order Functions

Preview — 3 of 10 questions

What best describes a closure in JavaScript?

javascript
function outer() {
  const message = "Hello";
  
  function inner() {
    console.log(message); // Accesses 'message' from outer scope
  }
  
  return inner;
}

const closureFunc = outer();
closureFunc(); // "Hello" — 'message' is still accessible!
AA function that is declared inside another function.
BA function that can access variables from its outer (enclosing) function even after the outer function has finished execution.
CA function that has no parameters.
DA function that can only be invoked once.

What will be the output of the following code?

javascript
function createCounter() {
  let count = 0;
  return function() {
    count++;
    console.log(count);
  };
}

const counter1 = createCounter();
counter1();
counter1();
A1, 1
B1, 2
Cundefined, undefined
DReferenceError

Which of the following is an example of currying?

javascript
// Traditional function
function add(a, b) {
  return a + b;
}
add(2, 3); // 5

// Curried function
function addCurried(a) {
  return function(b) {
    return a + b;
  };
}
addCurried(2)(3); // 5
Aadd(2)(3);
Badd(2, 3);
Cadd(2) + 3;
Dadd(3)(2);

Sign up free to play

Answer all 10 questions (7 more), see explanations for every answer, and track your score.