A function is a reusable block of code that performs a specific task.


Function

  • A function allows you to write code once and use it many times

  • Helps with modularity, readability, and maintainability

  • Functions are first-class citizens in JavaScript


2️⃣ Function Declaration (Traditional Function)

Syntax

function functionName(parameters) {
// code
}

Example

function greet() {
console.log("Hello World");
}

greet(); // Call the function

✔ Hoisted (can be called before definition)


3️⃣ Function Parameters & Arguments

function add(a, b) {
return a + b;
}

console.log(add(5, 3)); // 8

  • Parameters → variables in function definition

  • Arguments → values passed to the function


4️⃣ Return Statement

function square(x) {
return x * x;
}

let result = square(4);
console.log(result); // 16

✔ Stops function execution
✔ Returns a value


5️⃣ Function Expression

const multiply = function (a, b) {
return a * b;
};

console.log(multiply(3, 4)); // 12

❌ Not hoisted
✔ Stored in a variable


6️⃣ Arrow Functions (ES6)

Syntax

const add = (a, b) => a + b;

Example

const greet = () => console.log("Hi!");

With Block

const sum = (a, b) => {
return a + b;
};

✔ Short syntax
❌ No this, arguments, super


7️⃣ Default Parameters

function greet(name = "Guest") {
console.log(`Hello ${name}`);
}

greet(); // Hello Guest


8️⃣ Rest Parameters

function total(...numbers) {
return numbers.reduce((sum, n) => sum + n, 0);
}

console.log(total(1, 2, 3)); // 6

✔ Collects multiple arguments into an array


9️⃣ Function Scope

function test() {
let x = 10;
console.log(x);
}

test();
// console.log(x); ❌ Error

✔ Variables inside function are local


🔟 Closures (Important Concept)

function outer() {
let count = 0;
return function inner() {
count++;
console.log(count);
};
}

const counter = outer();
counter(); // 1
counter(); // 2

✔ Inner function remembers outer variables


1️⃣1️⃣ Callback Functions

A function passed as an argument to another function.

function process(fn) {
fn();
}

process(() => console.log("Callback called"));


1️⃣2️⃣ Higher-Order Functions

Functions that:

  • Accept functions

  • Return functions

function multiplier(x) {
return function (y) {
return x * y;
};
}

const double = multiplier(2);
console.log(double(5)); // 10


1️⃣3️⃣ Immediately Invoked Function Expression (IIFE)

(function () {
console.log("Runs immediately");
})();

✔ Used to avoid global scope pollution


1️⃣4️⃣ Function Hoisting

hello();

function hello() {
console.log("Hello");
}

✔ Works because function declarations are hoisted


1️⃣5️⃣ Named vs Anonymous Functions

// Named
function test() {}

// Anonymous
const fn = function () {};


1️⃣6️⃣ Recursive Functions

Function calling itself.

function factorial(n) {
if (n === 1) return 1;
return n * factorial(n - 1);
}

console.log(factorial(5)); // 120


1️⃣7️⃣ Function as Object

function demo() {}
demo.version = "1.0";

console.log(demo.version);

✔ Functions can have properties

Categorized in:

Javascript,