Create Hello World Function - Problem

Write a function createHelloWorld. It should return a new function that always returns "Hello World".

This problem introduces the concept of closures in JavaScript - functions that have access to the outer (enclosing) scope's variables even after the outer function has returned.

Input & Output

Example 1 — Basic Function Creation
$ Input: createHelloWorld()
Output: function that returns "Hello World"
💡 Note: createHelloWorld() returns a new function. When that returned function is called, it outputs "Hello World"
Example 2 — Multiple Calls
$ Input: const f = createHelloWorld(); f(); f();
Output: "Hello World", "Hello World"
💡 Note: The same returned function can be called multiple times, always returning the same string
Example 3 — Multiple Instances
$ Input: const f1 = createHelloWorld(); const f2 = createHelloWorld();
Output: Two independent functions, both return "Hello World"
💡 Note: Each call to createHelloWorld() creates a new function instance, but they all behave identically

Constraints

  • The returned function should always return the exact string "Hello World"
  • No parameters are passed to createHelloWorld or the returned function

Visualization

Tap to expand
Create Hello World Function OverviewINPUTCall createHelloWorld()No parameters neededPROCESSCreate inner functionReturn the functionOUTPUTFunction that returns"Hello World"When returned function is called:"Hello World"💡 This demonstrates higher-order functions and closures
Understanding the Visualization
1
Input
Call createHelloWorld() with no parameters
2
Process
Create and return inner function
3
Output
Returned function always outputs "Hello World"
Key Takeaway
🎯 Key Insight: Functions can return other functions, creating flexible and reusable code patterns
Asked in
Google 15 Facebook 12 Amazon 8 Microsoft 6
156.0K Views
High Frequency
~5 min Avg. Time
2.8K Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen