🔗 LC2667 🟢 Easy 🧩 Pattern – Closures and Higher Order Functions
📅 Day 1/30 Days of JavaScript
Write a function createHelloWorld
. It should return a new function that always returns "Hello World"
.
Example
Input: args = []
Output: "Hello World"
Explanation:
const f = createHelloWorld();
f(); // "Hello World"
Solution
While this is a straightforward question, it’s important to understand key JavaScript concepts such as Higher Order Functions, Closures, and Hoisting to fully understand the solution.
createHelloWorld is a higher-order function, as it returns a function.
Higher Order Function: A function that accepts functions as an argument or returns a function.
/**
* @return {Function}
*/
var createHelloWorld = function () {
return function () {
return "Hello World"
}
};
/**
* const f = createHelloWorld();
* f(); // "Hello World"
*/