Functions in Node.js
Complete and in-depth guide to JavaScript functions: creation, execution flow, parameters, closures, IIFE, tagged templates, and higher-order functions.
Function Basics & Execution Flow
Functions encapsulate reusable logic. When a function is called, execution jumps into the function and returns back after completion.
Creating and Calling a Function
Define once, call many times.
Function Execution Flow
Understand call order.
Parameters & Arguments
Parameters receive input values called arguments.
Optional / Default Parameters
Provide fallback values.
Named Parameters (Object)
Order-independent arguments.
Unlimited Parameters
Rest parameters collect arguments into an array.
Rest Parameters
Unlimited inputs.
Passing Array to Rest Parameters
Use spread operator.
Function Forms
JavaScript supports anonymous, arrow, and self-invoking functions.
Anonymous Functions
Functions without names.
Self Invoking Function (IIFE)
Runs immediately.
Arrow Functions
Shorter syntax.
Closures & State
Closures allow functions to access variables from outer scopes even after execution.
Stateful Closure
Maintain private state.
Template Literal Tag Functions
Tagged templates allow custom processing of template literals.
Tagged Template Function
Intercept template literals.
Higher Order Functions (Custom Implementation)
A higher order function is a function that takes another function as an argument, returns a function, or both. To truly understand them, we implement common higher order functions manually using basic loops.
1. What is a Higher Order Function?
Functions that operate on other functions.
2. Creating map() from Scratch
Transform each element.
3. Creating filter() from Scratch
Select elements conditionally.
4. Creating forEach() from Scratch
Execute logic for each element.
5. Creating find() from Scratch
Find first matching element.
6. Creating findIndex() from Scratch
Find index of matching element.
7. Creating some() from Scratch
Check if at least one element matches.
8. Creating every() from Scratch
Check if all elements match.
9. Creating reduce() from Scratch
Accumulate values into one.
10. Creating group() from Scratch
Group elements based on multiple rules.