Skip to main content

Functions

What is a Function?​

Think of a function as a reusable recipe or a mini-program within your program. Just like a recipe you can use over and over, functions let you write code once and use it many times!

Real-World Analogy πŸ³β€‹

Recipe: Make Coffee
─────────────────────
Inputs: coffee grounds, water
Steps:
1. Heat water
2. Add grounds to filter
3. Pour water through
Output: Fresh coffee! β˜•

Every time you follow this recipe, you get coffee. Similarly, every time you call a function, it performs its task!

Functions in JavaScript​

Defining a Function
function makeGreeting(name) {
// Input: name
const greeting = "Hello, " + name + "!";
return greeting; // Output
}

// Use the function multiple times
console.log(makeGreeting("Alice")); // "Hello, Alice!"
console.log(makeGreeting("Bob")); // "Hello, Bob!"
console.log(makeGreeting("Charlie")); // "Hello, Charlie!"

Why Functions Are Awesome:

  • ♻️ Reusable - Write once, use many times
  • πŸ“¦ Organized - Keep related code together
  • πŸ› Easier to debug - Fix bugs in one place
  • πŸ§ͺ Testable - Test each function separately
  • πŸ“– Readable - Clear names explain what code does

Anatomy of a Function πŸ”β€‹

function calculateArea(width, height) {
// ↑ ↑ ↑ ↑
// keyword name parameters (inputs)

const area = width * height; // Function body (logic)
return area; // Output (what function gives back)
}

// Using the function
const roomArea = calculateArea(10, 20); // Arguments (actual values)
// ↑ ↑
// 10, 20 are arguments
console.log(roomArea); // 200

Visual Breakdown:

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ function calculateArea(width, height)
β”‚ { β”‚
β”‚ const area = width * height; β”‚ ← Function body
β”‚ return area; β”‚ ← Return statement
β”‚ } β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
↓ Call it
calculateArea(10, 20)
↑ ↑
Arguments passed in
↓
Returns: 200

Simple Example: Step by Step​

Complete Example with Comments
// Step 1: Define the function (create the recipe)
function addNumbers(num1, num2) {
const sum = num1 + num2;
return sum;
}

// Step 2: Call the function (use the recipe)
const result = addNumbers(5, 3); // result = 8

// Step 3: Use the result
console.log(result); // Output: 8

// You can call it again with different values
console.log(addNumbers(10, 20)); // Output: 30
console.log(addNumbers(100, 50)); // Output: 150

Default Parameters (Optional Inputs) πŸŽβ€‹

Sometimes you want a function to work even if some inputs are missing. Default parameters provide backup values!

Default Parameters
function greetUser(name = "Guest") {
return "Welcome, " + name + "!";
}

console.log(greetUser("Alice")); // "Welcome, Alice!"
console.log(greetUser()); // "Welcome, Guest!" (uses default)

Real-World Example:

function calculatePrice(price, taxRate = 0.08) {
const tax = price * taxRate;
const total = price + tax;
return total;
}

// Tax rate provided
console.log(calculatePrice(100, 0.10)); // 110 (10% tax)

// Tax rate NOT provided - uses default 8%
console.log(calculatePrice(100)); // 108 (8% tax)

Multiple Default Parameters:

function createUser(name = "Anonymous", role = "user", active = true) {
return {
name: name,
role: role,
active: active
};
}

console.log(createUser("Alice", "admin"));
// { name: "Alice", role: "admin", active: true }

console.log(createUser("Bob"));
// { name: "Bob", role: "user", active: true }

console.log(createUser());
// { name: "Anonymous", role: "user", active: true }
When to Use Default Parameters
  • Optional configuration settings
  • Fallback values when data might be missing
  • Making functions more flexible and forgiving
What is parameter, argument, and return value?

Parameter vs Argument​

A parameter is a variable that is used to store the value of an argument. An argument is a value that is passed to a function when it is called. A return value is the value that is returned by a function when it is called.

For example, in the calculateArea function, the width and height parameters are used to store the values of the arguments that are passed to the function when it is called.

function calculateArea(width, height) {
return width * height;
}

const area = calculateArea(10, 20);
console.log(area); // 200

Here, 10 and 20 are the arguments that are passed to the calculateArea() function when it is called. The width and height parameters are used to store the values of the arguments.

Return value​

A function can return a value using the return keyword. The return value is the value that is returned by the function when it is called. For example:

function calculateArea(width, height) {
return width * height;
}

const area = calculateArea(10, 20);
console.log(area); // 200

Types of Functions​

There are several types of functions, depending on how they are defined and how they are used. Here are a few examples:

Function declarations​

These are the most common (old) way to define a function, and they are hoisted to the top of the code. They have the following syntax:

Function declaration
function functionName(parameters) {
// code to be executed
}

Function declarations are hoisted, which means that you can call a function even before it is defined. For example:

Function declaration
greet(); // This works, and prints 'Hello, world!'

//
function greet() {
console.log("Hello, world!");
}

// This also works and prints 'Hello, world!'
greet();

In this example, the greet() function is called before it is defined, but it still works because function declarations are hoisted.

What is hoisting?

You will read about it in the advanced section of JavaScript. Let's just briefly explain it here.

Hoisting is the mechanism that moves declarations to the top of the code before execution which means that you can call them before it is defined (or declared)

Function Expressions​

Function expressions are another way to define a function, in which a function is assigned to a variable. They have the following syntax:

Function expression
const functionName = function (parameters) {
// code to be executed
};

Function expressions are not hoisted, which means that you can't call a function before it is defined. For example:

greet(); // ReferenceError: Cannot access 'greet' before initialization

const greet = function () {
console.log("Hello, world!");
};

In this example, the greet() function is called before it is defined, and it throws an error.

Arrow Functions​

Arrow functions are a concise way to define a function. It is available in modern browsers and Node.js. They have the following syntax:

Arrow function
const functionName = (parameters) => {
// code to be executed
};

You can see no function keyword, and the arrow => is used instead. Arrow functions are not hoisted, which means that you can't call a function before it is defined. For example:

greet(); // ReferenceError: Cannot access 'greet' before initialization

const greet = (x) => {
console.log("Hello, world!");
};

Here are a few examples of arrow functions:

A simple arrow function that returns the square of a number:

const square = (x) => {
return x * x;
};

console.log(square(10)); // 100

An arrow function that returns the sum of two numbers:

const add = (x, y) => {
return x + y;
};

console.log(add(10, 20)); // 30

An arrow function that returns the maximum of two numbers:

const max = (x, y) => {
if (x > y) {
return x;
} else {
return y;
}
};

console.log(max(10, 20)); // 20

If the body of the arrow function consists of a single statement that returns a value, omit the curly braces and the return keyword, like this:

const square = (x) => x * x; // This will return x * x
const add = (x, y) => x + y; // This will return x + y
const max = (x, y) => (x > y ? x : y); // This will return x if condition is true, else y

console.log(square(10)); // 100
console.log(add(10, 20)); // 30
console.log(max(10, 20)); // 20

Anonymous functions​

Anonymous functions don't have a name, and they are mostly used as callbacks functions which means we define, and pass them as argument to other function.

They have the following syntax:

function (parameters) {
// code to be executed
}

Here are a few examples of anonymous functions:

An anonymous function that calculates the square of a number:

const square = function (x) {
return x * x;
};

An anonymous function that sorts an array in ascending order:

const numbers = [3, 1, 2];

numbers.sort(
function (a, b) {
return a - b;
},
);

console.log(numbers); // prints [1, 2, 3]

IIFE (Immediately Invoked Function Expression)​

An IIFE (Immediately Invoked Function Expression) is a JavaScript function that runs as soon as it is defined. They have the following syntax:

(function () {
// code to be executed
})();

IIFE functions are useful when you need to define a function that is used only once, and you want to avoid polluting the global namespace.

Here are a few examples of IIFE functions:

An IIFE function that prints a message to the console:

(function () {
console.log("Hello, world!");
})();

An IIFE function that calculates the square of a number:

const square = (function (x) {
return x * x;
})(10);

console.log(square); // prints 100

An IIFE function that sorts an array in ascending order:

const numbers = [3, 1, 2];

numbers.sort(
function (a, b) {
return a - b;
}
);

console.log(numbers); // prints [1, 2, 3]
note

IIFE functions are not very common in modern JavaScript code, but they are still useful when you need to define a function that is used only once, and you want to avoid polluting the global namespace.

Now what is global namespace?

Global namespace is the space where all the variables and functions are defined. It is the global scope. In JavaScript, the global scope is the default scope. This means that all variables and functions are defined in the global scope unless you specify the scope.

namespace is a collection of variables, functions, and objects. It is a container for variables and functions. In JavaScript, the global namespace is the global object. The global object is the default object of the global scope. In a browser, the global object is the window object. In Node.js, the global object is the global object.

Function Overloading​

Function overloading is a feature that allows you to define multiple functions with the same name but with different parameters. The JavaScript language doesn't support function overloading as it treats functions as objects, and all objects have unique names. Let me explain this with examples.

Let's say we want to define a function that calculates the sum of two and three numbers. We can define two functions with the same name but with different parameters:

function sum(x, y) {
return x + y;
}

function sum(x, y, z) {
return x + y + z; // Silently overwrites the first definition!
}

console.log(sum(10, 20)); // NaN β€” only the 3-argument version exists now

Unlike most languages, JavaScript does not throw an error when you redeclare a function β€” the second definition simply replaces the first, which makes overloading impossible this way. Using const or let will throw an error:

const sum = function (x, y) {
return x + y;
};

const sum = function (x, y, z) {
// SyntaxError: Identifier 'sum' has already been declared
return x + y + z;
};

The same error applies to let and arrow functions.

const sum = function (x, y) {
return x + y;
};

const sum = function (x, y, z) {
// SyntaxError: Identifier 'sum' has already been declared
return x + y + z;
};

But we can use the var keyword to define the function, and it will not throw any error.

var sum = function (x, y) {
return x + y;
};

var sum = function (x, y, z) {
return x + y + z;
};

The reason is that the var keyword allows you to redeclare the same variable multiple times. It will replace the old value with the new value. So, every time it will call the last defined function.

var sum = function (x, y) {
return "Two arguments";
};

var sum = function (x, y, z) {
return "Three arguments";
};

console.log(sum(10, 20)); // prints 'Three arguments'
console.log(sum(10, 20, 30)); // prints 'Three arguments'

So, you can see that the last defined function is called every time.

You may wonder what to do if you want to define multiple functions with the same name but with different parameters. The answer is that you can use the arguments object to access the arguments passed to a function.

The arguments object is an array-like object that contains the arguments passed to a function. It has the following syntax:

arguments[index];

Here is an example of using the arguments object:

function sum() {
let total = 0;

for (let i = 0; i < arguments.length; i++) {
total += arguments[i];
}

return total;
}

console.log(sum(10, 20)); // prints 30
console.log(sum(10, 20, 30)); // prints 60

You can also use the rest parameter syntax to access the arguments passed to a function. The rest parameter syntax allows you to represent an indefinite number of arguments as an array. It has the following syntax:

function sum(...args) {
let total = 0;

for (let i = 0; i < args.length; i++) {
total += args[i];
}

return total;
}

console.log(sum(10, 20)); // prints 30
console.log(sum(10, 20, 30)); // prints 60

Real-World Function Examples πŸŒβ€‹

Example 1: Form Validation​

function validateEmail(email) {
if (!email.includes("@")) {
return false;
}
if (email.length < 5) {
return false;
}
return true;
}

console.log(validateEmail("user@example.com")); // true
console.log(validateEmail("invalid")); // false
console.log(validateEmail("a@b")); // false

Example 2: Calculate Discount​

function calculateDiscount(price, discountPercent) {
const discountAmount = price * (discountPercent / 100);
const finalPrice = price - discountAmount;
return finalPrice;
}

const originalPrice = 100;
const salePrice = calculateDiscount(originalPrice, 20);
console.log(salePrice); // 80 (20% off)

Example 3: Format Currency​

function formatCurrency(amount) {
return "$" + amount.toFixed(2);
}

console.log(formatCurrency(19.5)); // "$19.50"
console.log(formatCurrency(100)); // "$100.00"
console.log(formatCurrency(5.999)); // "$6.00"

Example 4: Check Age Eligibility​

function canVote(age) {
if (age >= 18) {
return "Eligible to vote";
} else {
const yearsUntilEligible = 18 - age;
return "Not eligible. Wait " + yearsUntilEligible + " more years";
}
}

console.log(canVote(20)); // "Eligible to vote"
console.log(canVote(16)); // "Not eligible. Wait 2 more years"

Example 5: Generate Random Number​

function getRandomNumber(min, max) {
const random = Math.random() * (max - min) + min;
return Math.floor(random);
}

console.log(getRandomNumber(1, 10)); // Random number between 1-10
console.log(getRandomNumber(50, 100)); // Random number between 50-100

Common Mistakes to Avoid πŸš¨β€‹

Mistake 1: Forgetting to Return​

// ❌ Wrong - Function doesn't return anything
function add(a, b) {
a + b; // Result is calculated but not returned!
}
const result = add(5, 3);
console.log(result); // undefined

// βœ… Right - Use return
function add(a, b) {
return a + b;
}
const result = add(5, 3);
console.log(result); // 8

Mistake 2: Calling Function Without Parentheses​

function greet() {
return "Hello!";
}

// ❌ Wrong - Missing parentheses
console.log(greet); // Logs the function itself, not the result

// βœ… Right - Include parentheses to call it
console.log(greet()); // "Hello!"

Mistake 3: Wrong Number of Arguments​

function calculate(a, b, c) {
return a + b + c;
}

// ❌ Providing wrong number of arguments
console.log(calculate(5, 3)); // NaN (c is undefined)
console.log(calculate(5, 3, 2, 1)); // 10 (extra arguments ignored)

// βœ… Provide correct number
console.log(calculate(5, 3, 2)); // 10

Practice Exercise βœοΈβ€‹

Try creating these functions yourself:

  1. isEven(number) - Returns true if number is even, false if odd
  2. getFullName(firstName, lastName) - Returns combined full name
  3. calculateTip(billAmount, tipPercent) - Returns tip amount
  4. convertToFahrenheit(celsius) - Converts Celsius to Fahrenheit (F = C Γ— 9/5 + 32)
Click to see solutions
// Solution 1
function isEven(number) {
return number % 2 === 0;
}

// Solution 2
function getFullName(firstName, lastName) {
return firstName + " " + lastName;
}

// Solution 3
function calculateTip(billAmount, tipPercent) {
return billAmount * (tipPercent / 100);
}

// Solution 4
function convertToFahrenheit(celsius) {
return celsius * 9/5 + 32;
}

Key Takeaways πŸŽ―β€‹

βœ… Functions are reusable blocks of code βœ… Use function keyword to declare βœ… Parameters = inputs, Return = output βœ… Call functions with parentheses () βœ… Default parameters provide fallback values βœ… Arrow functions => are modern and concise βœ… Always return a value if you need output βœ… Functions make code organized and DRY (Don't Repeat Yourself)

Conclusion​

In this article, we learned about functions in JavaScriptβ€”one of the most powerful and important concepts in programming. We explored function declarations, function expressions, arrow functions, anonymous functions, and IIFE functions. We also covered real-world examples, common mistakes, and best practices.

Remember: Functions are like tools in your toolbox. The more comfortable you become with them, the more powerful your code will be! πŸš€