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
Example
✔ Hoisted (can be called before definition)
3️⃣ Function Parameters & Arguments
-
Parameters → variables in function definition
-
Arguments → values passed to the function
4️⃣ Return Statement
✔ Stops function execution
✔ Returns a value
5️⃣ Function Expression
❌ Not hoisted
✔ Stored in a variable
6️⃣ Arrow Functions (ES6)
Syntax
Example
With Block
✔ Short syntax
❌ No this, arguments, super
7️⃣ Default Parameters
8️⃣ Rest Parameters
✔ Collects multiple arguments into an array
9️⃣ Function Scope
✔ Variables inside function are local
🔟 Closures (Important Concept)
✔ Inner function remembers outer variables
1️⃣1️⃣ Callback Functions
A function passed as an argument to another function.
1️⃣2️⃣ Higher-Order Functions
Functions that:
-
Accept functions
-
Return functions
1️⃣3️⃣ Immediately Invoked Function Expression (IIFE)
✔ Used to avoid global scope pollution
1️⃣4️⃣ Function Hoisting
✔ Works because function declarations are hoisted
1️⃣5️⃣ Named vs Anonymous Functions
1️⃣6️⃣ Recursive Functions
Function calling itself.
1️⃣7️⃣ Function as Object
✔ Functions can have properties
