Advanced Concepts
- This lesson covers advanced function-related concepts in JavaScript.
Introduction to Advanced Function Concepts
In earlier lessons, we learned:
What functions are
How to define and call functions
Parameters and return values
As JavaScript applications grow larger, we need:
Reusable logic
Predictable behavior
Clean and maintainable code
Advanced function concepts help us:
Write flexible programs
Follow functional programming principles
Improve code quality
In this lesson, we will cover:
Higher-Order Functions
Pure vs Impure Functions (Introduction)
Why Are Higher-Order Functions Important ?
Higher-order functions allow:
Code reusability
Cleaner and shorter code
Separation of logic
Functional programming style
They help avoid:
Repeated logic
Hard-coded behavior
Complex and unreadable code
Real-Life Example
Think of a remote control:
The remote does not know what the TV will show
It only knows how to control actions
Similarly:
Higher-order function controls behavior
Actual logic is passed as a function
Higher-Order Function
Uses a function as a parameter to make code flexible and reusable.
// Function as parameter (Higher-order function)
function greet(name) {
return "Hello " + name;
}
function processUser(name, callback) {
return callback(name);
}
console.log(processUser("Rahul", greet)); // Hello Rahul
Explanation
greet is a normal function
processUser is a higher-order function
greet is passed as an argument
Function Returning Another Function
Creates a function that generates customized functions based on input.
// Function returning another function
function multiplier(factor) {
return function (number) {
return number * factor;
};
}
const double = multiplier(2);
console.log(double(5)); // 10
Explanation
multiplier returns another function
double stores the returned function
Higher-order behavior achieved
Built-in Higher-Order Functions
JavaScript provides many built-in higher-order functions, such as:
map()
filter()
reduce()
forEach()
map Function
Transforms each element of an array using a function.
// Using map()
let numbers = [1, 2, 3, 4];
let squares = numbers.map(function (num) {
return num * num;
});
console.log(squares); // [1, 4, 9, 16]
Advantages of Higher-Order Functions
Improved readability
Better abstraction
Less repetitive code
Easier debugging
Cleaner logic separation
Pure vs Impure Functions
What is a Pure Function ?
A pure function is a function that:
Always returns the same output for the same input
Does not change any external state
Does not depend on external variables
Pure Function
Always returns the same output for the same input without side effects.
// Pure function
function add(a, b) {
return a + b;
}
console.log(add(2, 3)); // 5
console.log(add(2, 3)); // 5
Why is this pure ?
Same input gives same output
No external variable used
No side effects
Benefits of Pure Functions
Easy to test
Predictable output
Easier debugging
Safe to reuse
Supports functional programming
What is an Impure Function ?
An impure function is a function that:
Depends on external state
Modifies external variables
Produces different output for same input
Impure Function
Produces different results by modifying external state.
// Impure function
let total = 0;
function addToTotal(num) {
total += num; // modifies external variable
return total;
}
Why is this impure ?
Uses external variable total
Changes external state
Output depends on previous calls
Impure Function
Returns different output each time because it depends on current time.
// Impure function using current time
function showTime() {
return new Date().toLocaleTimeString();
}
Each call gives a different result.
Pure vs Impure Function Comparison
Feature Pure Function Impure Function Output Same for same input May change External state Not used Used Side effects None Present Testing Easy Difficult Predictability High Low When to Use Pure Functions ?
Calculations
Data transformation
Business logic
Utility functions
When Are Impure Functions Needed ?
DOM manipulation
API calls
Logging
Updating global state
Reading time or user input
Impure functions are sometimes necessary.
Relationship Between HOF and Pure Functions
Higher-order functions often use pure functions as callbacks
Pure functions make higher-order functions more reliable
Functional programming encourages both concepts
Common Mistakes
Confusing higher-order functions with callbacks
Modifying external variables inside functions
Expecting pure behavior from impure functions
Overusing impure logic
Best Practices
Prefer pure functions whenever possible
Use higher-order functions for reusable logic
Avoid unnecessary side effects
Keep functions small and focused
Separate logic from side effects