JavaScript Functions – Introduction
Last Updated: 21st October 2025
- A function is a block of code designed to perform a specific task.
- Functions only run when called.
- They help you organize code and reuse logic without rewriting it.
Hinglish Tip 🗣: Function ko aise samjho—ek “machine” jisme input (parameters) jaata hai aur output (return value) milta hai. Ek baar banao, baar-baar use karo.
Why Use Functions?
- Reusability – Write once, use many times.
- Modularity – Divide your code into smaller, manageable blocks.
- Readability – Makes code easier to read and maintain.
- Abstraction – Hide complex logic behind a simple function call.
Real-Life Analogy
Imagine a coffee machine:
- Input → Coffee powder + water + milk
- Process → Machine brews the coffee
- Output → Cup of coffee ready
Similarly, in JS:
function makeCoffee(coffeeType) {
return `Your ${coffeeType} coffee is ready!`;
}
console.log(makeCoffee("Espresso"));
// Output: Your Espresso coffee is ready!
Hinglish Tip 🗣: Jaise coffee machine me alag-alag inputs daal kar alag-alag coffee banate hain, JS me function me arguments daal kar alag outputs lete hain.
Type of Functions
There are two types of functions:
- User-Defined Functions: Created by you.
- Built-in Functions: Part of the JavaScript Standard Library.
User-Defined Functions
- Non Parameterized Function
- Parameterized Function
- Return Function
- Nested Function
- Closure
- Recursive Function
- Callback Function
- Generator
- Higher-Order Function
- Immediately Invoked Function Expression
Built-in Functions
Built-in functions are part of the JavaScript Standard Library and you can use them without installing any additional packages.
Some of them are: console.log(), document.getElementById(), setTimeout(), setInterval(), etc.