Codecraft
Codecraft
JavaScript

Functions

8 min read📖 Beginner
A function is a reusable block of code that performs a task. You define it once and call it as many times as you need, with different inputs each time.

Functions are the building blocks of JavaScript programs. Instead of repeating code, you wrap it in a function and call it by name.

Function Declarations

The classic way to define a function:

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

console.log(greet("Alice"));   // "Hello, Alice!"
console.log(greet("Bob"));     // "Hello, Bob!"

The value after return is sent back to wherever the function was called.

Parameters and Arguments

Parameters are the variables listed in the function definition. Arguments are the actual values passed when calling the function.

// name and age are parameters
function introduce(name, age) {
  return `${name} is ${age} years old.`;
}

// "Alice" and 25 are arguments
console.log(introduce("Alice", 25));

Default Parameters

You can provide fallback values for parameters that aren't passed:

function greet(name = "stranger") {
  return `Hello, ${name}!`;
}

console.log(greet("Alice"));   // "Hello, Alice!"
console.log(greet());          // "Hello, stranger!"

Function Expressions

You can store a function in a variable:

const double = function(n) {
  return n * 2;
};

console.log(double(5));   // 10

Arrow Functions

Arrow functions are a shorter syntax for function expressions. They're very common in modern JavaScript:

// Traditional
const square = function(n) {
  return n * n;
};

// Arrow function
const square = (n) => {
  return n * n;
};

// Concise arrow (single expression — return is implicit)
const square = (n) => n * n;

console.log(square(4));   // 16
💡

If an arrow function has exactly one parameter and one expression, you can drop both the parentheses and the curly braces: const double = n => n * 2;

Functions Calling Functions

function add(a, b) {
  return a + b;
}

function addAndDouble(a, b) {
  const sum = add(a, b);
  return sum * 2;
}

console.log(addAndDouble(3, 4));   // 14

Scope

Variables declared inside a function are local to that function — they can't be accessed outside it:

function calculate() {
  const result = 42;   // local variable
  return result;
}

console.log(result);   // ReferenceError: result is not defined

Higher-Order Functions

Functions can accept other functions as arguments. This is a key pattern in JavaScript:

const numbers = [1, 2, 3, 4, 5];

// map — transform each item
const doubled = numbers.map(n => n * 2);
console.log(doubled);   // [2, 4, 6, 8, 10]

// filter — keep items that pass a test
const evens = numbers.filter(n => n % 2 === 0);
console.log(evens);     // [2, 4]

// reduce — combine all items into one value
const sum = numbers.reduce((total, n) => total + n, 0);
console.log(sum);       // 15

Have anything to say about this lesson?

Your feedback helps improve these tutorials. If something was confusing or missing, let us know.

We don't currently reply to feedback — but if we add that feature in the future, we'll reach out to you.