JavaScript Notes
Practice JavaScript functions with 20+ hands-on programs covering arrow functions, closures, HOFs, recursion, IIFE, callbacks, and real-world coding challenges with solutions.
Overview
This module contains 20+ hands-on practice programs covering every topic in the JavaScript Functions module. Work through each problem before looking at the solution. The best way to learn is to struggle first, then study the answer.
Topics covered:
- Function Declaration & Expression
- Arrow Functions
- Callbacks
- Higher-Order Functions (map, filter, reduce)
- Closures
- IIFE
- Recursion
Program 1 – Basic Function Declaration
Task: Write a function greet that takes a name and returns "Hello, [name]! Welcome to JavaScript.".
function greet(name) {
return `Hello, ${name}! Welcome to JavaScript.`;
}
console.log(greet("Riya"));
console.log(greet("Arjun"));Hello, Riya! Welcome to JavaScript. Hello, Arjun! Welcome to JavaScript.
Program 2 – Arrow Function Conversion
Task: Convert this regular function to an arrow function:
// Original
function square(n) {
return n * n;
}Solution:
16 81
Program 3 – Default Parameters
Task: Write a function calculateTax(amount, rate = 0.18) that computes tax.
function calculateTax(amount, rate = 0.18) {
return amount * rate;
}
console.log(calculateTax(1000)); // uses default 18%
console.log(calculateTax(2000, 0.28)); // uses custom 28%180 560
Program 4 – Rest Parameters
Task: Write a function sum that accepts any number of arguments and returns their total.
6 150 7
Program 5 – Function Expression as Callback
Task: Use Array.forEach with a function expression to print items with their index.
1. apple 2. banana 3. mango 4. orange
🟡 Intermediate Level
Program 6 – Higher-Order Functions: Map + Filter + Reduce Chain
Task: Given a list of students with names and scores, find the average score of students who scored above 60.
Passed students: [88, 72, 91, 65] Average score: 79.00
Program 7 – Closure: Counter Factory
Task: Build a createCounter function that returns an object with increment, decrement, and reset methods. Each counter should be independent.
11 12 1 11 10 1
Program 8 – Arrow Function: this Context
Task: Demonstrate the difference between regular function and arrow function this binding inside an object.
Hi, I'm Priya, age 25 Hi, I'm undefined, age undefined
Takeaway: Use regular functions for object methods that need this.
Program 9 – Function Factory with Closures
Task: Create a makeAdder function that returns an adder function with a preset value.
function makeAdder(base) {
return function (number) {
return base + number;
};
}
const add5 = makeAdder(5);
const add10 = makeAdder(10);
const add100 = makeAdder(100);
console.log(add5(3)); // 5 + 3
console.log(add10(7)); // 10 + 7
console.log(add100(42)); // 100 + 42
console.log(add5(add10(2))); // 5 + (10 + 2) = 178 17 142 17
Program 10 – Custom map and filter
Task: Implement your own myMap and myFilter functions.
[4, 16, 36, 64, 100]
Program 11 – Callback: Custom forEach
Task: Write your own forEach function that works like Array.forEach.
Index 0: a Index 1: b Index 2: c
Program 12 – IIFE: Configuration Object
Task: Use an IIFE to create a configuration object with private defaults.
https://api.myapp.com/v2 3 https://api.myapp.com/v2/users undefined
🔴 Advanced Level
Program 13 – Recursion: Factorial
Task: Write a recursive factorial(n) function.
function factorial(n) {
if (n < 0) throw new Error("No factorial for negative numbers");
if (n <= 1) return 1;
return n * factorial(n - 1);
}
console.log(factorial(0));
console.log(factorial(5));
console.log(factorial(10));1 120 3628800
Program 14 – Recursion: Fibonacci with Memoization
Task: Write an efficient Fibonacci function using memoization.
0 1 1 2 3 5 8 13 21 34 55 fib(50): 12586269025
Program 15 – Recursion: Flatten Nested Array
Task: Write a recursive function to flatten a deeply nested array.
[1, 2, 3, 4, 5, 6, 7, 8]
Program 16 – Memoize Higher-Order Function
Task: Build a general memoize function that caches results of any function.
8 [Cache hit] args: [5,3] 8 12
Program 17 – Currying
Task: Write a curry function that converts a 3-argument function into curried form.
6 6 6 6
Program 18 – Debounce
Task: Build a debounce function using closures. Debounce ensures a function only fires after the user stops calling it for a set delay.
Searching for: JavaScript
Program 19 – Function Composition (pipe)
Task: Build a pipe function that applies functions left-to-right.
Hello, Riya Sharma! Hello, Arjun Verma!
Program 20 – Recursive Tree Traversal
Task: Count total nodes in a nested tree structure.
Total nodes: 7
🏆 Challenge Problems
Challenge 1 – Private Score Tracker
Build a score tracker using closures with add, subtract, reset, and history (returns all past scores).
10
15
12
12
[
{ action: '+10', score: 10 },
{ action: '+5', score: 15 },
{ action: '-3', score: 12 }
]Challenge 2 – Recursive Palindrome Check
true true false
Challenge 3 – Once Function (Runs Only Once)
function once(fn) {
let called = false;
let result;
return function (...args) {
if (!called) {
called = true;
result = fn(...args);
}
return result;
};
}
const initializeApp = once(function () {
console.log("App initialized!");
return "initialized";
});
console.log(initializeApp()); // runs
console.log(initializeApp()); // returns cached result
console.log(initializeApp()); // returns cached resultApp initialized! initialized initialized initialized
Key Takeaways
- ✅ Practice every topic: declaration, expression, arrow, callback, HOF, closure, IIFE, recursion
- ✅ Map/filter/reduce chains are a staple of real-world JavaScript
- ✅ Closures power counters, factories, debounce, memoize, and module patterns
- ✅ Recursion needs a base case and must shrink the problem with each call
- ✅ Higher-order patterns (curry, compose, pipe) are interview and codebase staples
- ✅ Build these programs from scratch before checking solutions
- ✅ The
once,memoize, anddebouncepatterns appear frequently in real codebases
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for JavaScript Functions Practice Programs – 20+ Exercises with Solutions.
Interview Use
Prepare one clear explanation, one practical example, and one common mistake for this JavaScript Master Course topic.
Search Terms
javascript-complete-guide, javascript master course, javascript, complete, guide, functions, practice, programs
Related JavaScript Master Course Topics