๐ฑ What is a Function? (First Principle)
A function is like a worker.
Just like a tea master in a shop:
- You give an order
- The tea master prepares tea
- The tea is given back to you
๐ In programming:
A function is a reusable block of code that takes input, performs a task, and optionally returns output.
โ Simple Example
function makeTea() {
console.log("Tea is ready");
}
makeTea();
๐ Here:
-
makeTea= worker - calling it = giving instruction
๐ฅ Functions with Input (Parameters)
Workers become more useful when they accept instructions.
function makeTea(sugar) {
console.log("Tea with " + sugar + " spoons of sugar");
}
makeTea(2);
makeTea(1);
๐ Same function, different results.
๐ค Functions with Output (Return Value)
Some workers not only do work โ they give results back.
function add(a, b) {
return a + b;
}
let result = add(5, 3);
๐ return sends the result back.
๐งพ Types of Functions
1. Function Declaration
function greet() {
console.log("Hello");
}
โ Can be used before it is written (hoisting)
2. Function Expression
const greet = function() {
console.log("Hello");
};
โ Cannot be used before definition
3. Arrow Function
const add = (a, b) => a + b;
โ Short and modern syntax
๐ง Functions are First-Class Citizens
In JavaScript:
Functions are treated like values.
That means:
- They can be stored in variables
- Passed as arguments
- Returned from other functions
Example:
function sayHello() {
return "Hello";
}
function execute(fn) {
console.log(fn());
}
execute(sayHello);
๐ Callback Functions
A callback is a function passed into another function.
function processTask(callback) {
console.log("Processing...");
callback();
}
processTask(() => {
console.log("Done!");
});
โณ Asynchronous Functions
JavaScript can run tasks later without stopping everything.
setTimeout(() => {
console.log("Task completed");
}, 2000);
๐ Promises
Promises make async code cleaner.
const promise = new Promise((resolve) => {
resolve("Success");
});
promise.then(result => console.log(result));
โจ Async/Await
Modern and easy way to handle async operations.
async function getData() {
let response = await fetch("url");
let data = await response.json();
return data;
}
๐ Closures
A closure allows a function to remember its outer variables.
function counter() {
let count = 0;
return function() {
count++;
return count;
};
}
const c = counter();
c(); // 1
c(); // 2
๐งญ Scope
Scope defines where variables can be accessed.
function outer() {
let x = 10;
function inner() {
console.log(x);
}
inner();
}
๐ญ The this Keyword
this refers to the object calling the function.
const user = {
name: "Jegan",
greet() {
console.log(this.name);
}
};
๐๏ธ Constructor Functions
Functions can create objects.
function User(name) {
this.name = name;
}
const u1 = new User("Jegan");
๐งช Higher-Order Functions
Functions that use other functions.
const numbers = [1, 2, 3];
const doubled = numbers.map(n => n * 2);
โ๏ธ Behind the Scenes
When a function runs:
- JavaScript creates an execution context
- Uses a call stack to manage execution
๐ Real-World Usage
Functions are used everywhere:
- Backend (APIs, controllers)
- Frontend (events, UI updates)
- Data processing
- Business logic
๐ง Final Understanding
A function is a reusable worker in your program.
It can:
- Take input
- Perform a task
- Return output
- Work with other functions
- Manage data and logic
๐ง Simple Definition (Child Level)
A function is a helper that does a job when you ask it.
๐ Conclusion
Functions are not just a feature of JavaScript โ
they are the foundation of everything you build.
If you master functions, you master JavaScript.
Top comments (0)