Functions
Learn what JavaScript functions are, how declarations, expressions, parameters, default values and return work, with clear examples.
On this page
1. What are functions?
In simple words, a function is a small machine. You give it data (arguments), it does some work inside, and it gives back a result through return.
Write a function once, then call it as many times as you need. This reusability is the foundation of every big JavaScript program.
2. Why does it matter?
Without functions, you have to copy and paste the same logic again and again. If you fix a bug in one place, the other copies still have the bug.
| Without functions | With functions |
|---|---|
| The same calculation copied everywhere | Write it once, call it everywhere |
| A bug fix has to be done in 5 places | A bug fix in only one place |
| Long, confusing code | Small, named pieces |
| Hard to test | Each function can be tested separately |
3. Where is it used?
| Area | What the function does |
|---|---|
| E-commerce | calculateTotal(cart) works out the cart total |
| Forms | isValidEmail(email) checks the input |
| UI | handleClick() runs when a button is clicked |
| Formatting | formatPrice(1500) makes Rs 1,500 |
| React | Every component is really just a function |
4. How does it work?
A function works in two parts: first it is defined, then it is called. Nothing runs when you define it, the code runs only on a call.
A parameter is the name written in the definition, and an argument is the actual value given at the call.
5. Code example
Function declaration and expression
function greet(name) {
return `Hello, ${name}!`;
}
const square = function (n) {
return n * n;
};
console.log(greet("Ali"));
console.log(square(4));Default parameters and early return
function calculateTotal(price, quantity = 1, discount = 0) {
if (price <= 0) {
return 0;
}
const subtotal = price * quantity;
return subtotal - subtotal * discount;
}
console.log(calculateTotal(500));
console.log(calculateTotal(500, 3, 0.1));Object parameter (named arguments)
function createUser({ name, role = "student", active = true }) {
return { name, role, active };
}
console.log(createUser({ name: "Sara" }));How does the code work?
| Item | Meaning |
|---|---|
function greet(name) | A function declaration, can be called anywhere in the file (hoisting) |
const square = function | A function expression, usable only after this line |
quantity = 1 | A default parameter, 1 is used if no value is given |
return 0 | Early return, the function stops here |
{ name, role = "student" } | Named arguments through object destructuring |
| Output | Hello, Ali!, 16, 500, 1350 |
Interview tip
If you do not write return in a function, it returns undefined. A declaration is fully hoisted, an expression is not. For details see Scope & Hoisting.
6. Common mistakes
Mistake: Forgetting to write return
Why? The function does its work but the caller gets undefined, and the code after that breaks.
Better: Return the value you need outside. console.log and return are two different things.
Mistake: Passing a reference instead of a call, or the other way around
Why? Writing handleClick() in onClick runs the function immediately instead of waiting for the click.
Better: Write handleClick when you want a reference, and handleClick() when you want the result.
Mistake: Too much work in one function
Why? A long function is hard to understand, test and reuse.
Better: Give each function one clear job, and let its name say that job, like formatPrice or isValidEmail.
Mistake: Too many positional parameters
Why? Reading createUser('Ali', true, false, 3), you cannot tell which value is for what.
Better: With more than 3 inputs, take one object parameter and destructure it.
7. What to learn before and after
| Stage | Topics |
|---|---|
| Before | Variables, Data Types, Conditionals |
| Alongside | Arrow Functions, Scope & Hoisting |
| Later | Closures, Higher-Order Functions, Callbacks |
Final quick revision
| Point | Detail |
|---|---|
| What a function is | A reusable, named block of code |
| Parameter vs argument | The name in the definition vs the value in the call |
| Default parameter | A fallback if the value is missing |
return | Gives back the result and stops the function |
Without return | The result is undefined |