Functions

Function declarations, arrow functions, default parameters, rest parameters and destructuring.

Runs in Zerethon IDE · Node.js 22 Official JavaScript documentation ↗
main.js
function greet(name, greeting = 'Hello') {
  return `${greeting}, ${name}!`;
}

const square = (x) => x * x;
const sum = (...numbers) => numbers.reduce((a, b) => a + b, 0);

function stats({ values, label = 'data' }) {
  return { label, min: Math.min(...values), max: Math.max(...values) };
}

function counter() {
  let n = 0;
  return () => ++n;
}

console.log(greet('Ada'), greet('Linus', 'Hi'));
console.log(square(9), sum(1, 2, 3));
console.log(stats({ values: [4, 8, 6] }));
const next = counter();
next();
console.log(next());

Output

Hello, Ada! Hi, Linus!
81 6
{ label: 'data', min: 4, max: 8 }
2
  • A function without return returns undefined.
  • Arrow functions are short and do not have their own this — good for callbacks.
  • Functions are values: pass them to map, filter, sort, or return them (a closure keeps its variables).