TExcelTAD
Home › JavaScript › JS Functions

JavaScript Functions

A function is a reusable block of code with a name. You define it once and call (or invoke) it whenever you need it, optionally passing in values and getting a result back. Functions are the main way to organise a program into small, understandable pieces.

Defining and calling a function

js · try it yourself
function greet(name) {
  return "Hello, " + name + "!";
}

console.log(greet("Maya"));
console.log(greet("Omar"));
Hello, Maya!
Hello, Omar!

The parts of a function declaration:

Part In the example Purpose
function keyword function starts the declaration
name greet how you call it
parameters (name) placeholders for the input values
body { … } the code that runs
return return "Hello, " … sends a value back to the caller

The values passed when calling ("Maya") are called arguments. Inside the function they're available through the parameter names.

Return values

return ends the function immediately and hands a value back. A function without return (or with a bare return;) gives undefined:

js · try it yourself
function square(x) {
  return x * x;
}
function logOnly(x) {
  console.log("Got", x);
}

const result = square(7) + square(2);
console.log(result);
console.log(logOnly(3));
53
Got 3
undefined

Parameters and arguments

JavaScript doesn't check how many arguments you pass. Missing ones become undefined; extras are ignored. Default parameters give a fallback value:

js · try it yourself
function makeTea(type = "green", sugar = 0) {
  return `${type} tea with ${sugar} sugar`;
}

console.log(makeTea());
console.log(makeTea("black"));
console.log(makeTea("mint", 2));
console.log(makeTea(undefined, 1));   // undefined triggers the default
green tea with 0 sugar
black tea with 0 sugar
mint tea with 2 sugar
green tea with 1 sugar

Functions are values

In JavaScript a function is a value like any other: you can store it in a variable, put it in an array, or pass it to another function. A function expression creates a function without a declaration, and an arrow function is a shorter syntax for the same idea:

js · try it yourself
const double = function (n) {
  return n * 2;
};
const triple = (n) => n * 3;

function applyTwice(fn, value) {
  return fn(fn(value));
}

console.log(double(5), triple(5));
console.log(applyTwice(double, 3));
console.log(applyTwice(triple, 2));
console.log(double);            // the function itself, not a call
10 15
12
18
[Function: double]

Note the difference: double is the function, double(5) calls it.

Tip: A function passed to another function (like fn above) is called a callback. Callbacks are everywhere in JavaScript — event handlers, array methods like map, timers.

Local variables

Variables declared inside a function are local: they exist only while the function runs and can't be seen from outside. Different functions can safely reuse the same names.

js · try it yourself
function priceWithTax(net) {
  const rate = 0.2;            // local to this function
  return net * (1 + rate);
}
console.log(priceWithTax(100));
console.log(typeof rate);      // not visible here
120
undefined

Why use functions?

  • Reuse — write logic once, call it from many places.
  • Naming — calculateShipping(order) explains itself better than ten lines of arithmetic.
  • Testing — small functions with clear inputs and outputs are easy to check.
  • Change in one place — fix a bug once and every caller benefits.

Function declarations are hoisted: you can call them before the line where they're defined. Function expressions and arrow functions stored in const are not — see the Hoisting chapter.

Test yourself

What does a function return if it has no return statement?

  1. 0
  2. null
  3. undefined
  4. An empty string

Given function f(a, b = 10) { return a + b; }, what is f(5)?

  1. 5
  2. 15
  3. NaN
  4. undefined

What is the difference between fn and fn()?

  1. None
  2. fn refers to the function, fn() calls it
  3. fn() refers to the function, fn calls it
  4. fn is an error