Skip to content
FunctionsChapter 13 / 47Beginner4 min read

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 functionsWith functions
The same calculation copied everywhereWrite it once, call it everywhere
A bug fix has to be done in 5 placesA bug fix in only one place
Long, confusing codeSmall, named pieces
Hard to testEach function can be tested separately

3. Where is it used?

AreaWhat the function does
E-commercecalculateTotal(cart) works out the cart total
FormsisValidEmail(email) checks the input
UIhandleClick() runs when a button is clicked
FormattingformatPrice(1500) makes Rs 1,500
ReactEvery 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.

Journey of a function call
Function defined (parameter names)
Function called (arguments given)
Parameters get the values of the arguments
The body code runs
The result comes back with return

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

js
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

js
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)

js
function createUser({ name, role = "student", active = true }) {
  return { name, role, active };
}

console.log(createUser({ name: "Sara" }));

How does the code work?

ItemMeaning
function greet(name)A function declaration, can be called anywhere in the file (hoisting)
const square = functionA function expression, usable only after this line
quantity = 1A default parameter, 1 is used if no value is given
return 0Early return, the function stops here
{ name, role = "student" }Named arguments through object destructuring
OutputHello, 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

Suggested order
Functions
Arrow Functions
Scope & Hoisting
Closures
Higher-Order Functions

Final quick revision

PointDetail
What a function isA reusable, named block of code
Parameter vs argumentThe name in the definition vs the value in the call
Default parameterA fallback if the value is missing
returnGives back the result and stops the function
Without returnThe result is undefined