Skip to content

JavaScript Tutorial Functional JavaScript

First-Class Functions in JavaScript

Lesson 202 of 258 2 min read By Deepak Paswan Updated Oct 4, 2026

In JavaScript, a function is a value, just like a number or a string. That is what “first-class” means: functions can be used anywhere a value can.

1. Stored in variables, arrays and objects

script.js
JS
const double = n => n * 2;
const operations = [double, n => n + 1, Math.sqrt];

console.log(operations.map(fn => fn(16))); // → [32, 17, 4]

const calculator = {
  add: (a, b) => a + b,
  multiply: (a, b) => a * b
};
console.log(calculator.multiply(3, 4));    // → 12

2. Passed as arguments

script.js
JS
function applyTo(numbers, transform) {
  return numbers.map(transform);
}

console.log(applyTo([1, 2, 3], n => n * 10)); // → [10, 20, 30]

3. Returned from functions

script.js
JS
function makeAdder(amount) {
  return function (n) { return n + amount; };
}

const addFive = makeAdder(5);
console.log(addFive(10)); // → 15

What this makes possible

A lot of everyday JavaScript depends on it:

  • Callbacks and event handlers: you hand a function to the browser.
  • Array methods: map, filter and sort receive your rules as functions.
  • Closures: returned functions remember where they came from (Closures).
  • Higher-order functions: functions that work with functions (Higher-Order Functions).
  • Strategies: choose behaviour by picking a function.
script.js
JS
const formatters = {
  currency: n => "$" + n.toFixed(2),
  percent: n => (n * 100).toFixed(0) + "%",
  plain: n => String(n)
};

function show(value, style) {
  return (formatters[style] ?? formatters.plain)(value);
}

console.log(show(5, "currency"));   // → $5.00
console.log(show(0.25, "percent")); // → 25%
console.log(show(7, "unknown"));    // → 7

Instead of a long switch statement, the code picks a function from an object.

Functions have properties too

Functions are objects, so they have a name and a length (their number of parameters), and you can attach your own properties.

script.js
JS
function greet(a, b) {}
console.log(greet.name, greet.length); // → greet 2

Comparing functions

Two functions with the same code are still different values:

script.js
JS
console.log((() => 1) === (() => 1)); // → false

Pass the function, do not call it

A very common bug is calling a function where you meant to pass it, as in setTimeout(run(), 100). See Functions.

info
Why "first-class"?
Languages where functions have these abilities treat them as full citizens. The idea comes from functional programming, and the next lessons show its most useful habits.