AnyCompiler

Arrow Functions

Arrow functions are a more compact way to write a function — JavaScript's equivalent of Python's lambda, though far more commonly used in everyday code than lambda is in Python.

const square = (x) => x * x;
console.log(square(5));
Output
25

(x) => x * x means "a function that takes x and returns x * x" — functionally identical to writing function square(x) { return x * x; }, just condensed to one line. When the body is a single expression, return is implicit — there's no return keyword and no curly braces needed.

Where arrow functions actually get used

You'll see arrow functions most often passed directly into array methods like map(), where defining a separate named function would be unnecessary ceremony for something this small.

const numbers = [1, 2, 3, 4, 5];
const squared = numbers.map((x) => x * x);
console.log(squared);
Output
[ 1, 4, 9, 16, 25 ]

Next: array methods — map, filter, and reduce, the idiomatic way to transform collections in JavaScript.

Practice exercise

Write an arrow function called double that multiplies its argument by 2, then print double(9).