Online Compiler logoOnline Compiler

JavaScript Tutorial

JavaScript Functions

Functions are the building blocks of JavaScript programs.

They let you organize logic, reuse behavior, and write cleaner code.

Why We Need It

Without functions, code becomes repetitive and difficult to maintain.

Functions help you structure programs and make your logic reusable.

Syntax

function name(params) { ... }
const name = function(params) { ... }
const name = (params) => { ... }

Basic Example

1. Basic function

function greet(name) {
  return "Hello " + name;
}

console.log(greet("Ava"));

Define a function once and reuse it.

Real World Example

2. Arrow function

const add = (a, b) => a + b;

console.log(add(2, 3));

Arrow functions are concise for small logic.

Multiple Use Cases

Reusable Logic

Functions let you package logic into reusable blocks. You can call them whenever you need the same behavior.

This keeps code DRY and easier to maintain.

  • Use functions for repeated tasks.
  • Name functions clearly to express intent.
  • Keep functions small and focused.

Inputs and Outputs

Parameters are inputs to a function. Return values are outputs.

Clear inputs and outputs make functions predictable and testable.

Function Styles

JavaScript supports declarations, expressions, arrow functions, and IIFEs.

Each style has different use cases for readability and context binding.

Callbacks and Higher-Order Functions

Callbacks let functions accept other functions as arguments.

Higher-order functions return functions or accept them, enabling powerful patterns.

More Examples

3. Callback

function run(task) {
  task();
}

run(() => console.log("Done"));

Pass functions as arguments for flexible behavior.

4. Higher-order

function makeMultiplier(factor) {
  return (value) => value * factor;
}

const double = makeMultiplier(2);
console.log(double(5));

Return functions to create reusable behaviors.

Comparison

Without

const price1 = 10 * 1.18;
const price2 = 20 * 1.18;

With

function addTax(amount) {
  return amount * 1.18;
}

const price1 = addTax(10);
const price2 = addTax(20);

Common Mistakes and Fixes

Doing too much in one function

Split logic into smaller, focused functions.

Ignoring return values

Return values make functions easier to compose.

Overusing global variables

Pass data via parameters instead of globals.

Confusing function styles

Choose the style that matches your use case and stick to it.

Interview Questions

What is the difference between parameters and arguments?

Parameters are named inputs; arguments are actual values passed in.

What is a higher-order function?

A function that takes or returns another function.

What is recursion?

A function calling itself to solve a smaller version of a problem.

Practice Problem

Practice: Write a function that takes a price and a tax rate and returns the total.

// TODO: implement addTax

One Possible Solution

function addTax(price, rate) {
  return price + price * rate;
}

console.log(addTax(100, 0.18));

Frequently Asked Questions

What is a function?

A reusable block of code that can take inputs and return outputs.

When should I use arrow functions?

When you need short functions or lexical this binding.

What is a callback?

A function passed as an argument to be called later.

Try It Yourself

Try calling the function with different names and see the output.