Home / Topic Library / JavaScript / Functions
⚡ JavaScript Topic Library

JavaScript Functions — Declarations, Arrows & Callbacks

Functions are values in JavaScript — you can store them, pass them, and return them. That single idea powers callbacks, array methods and everything async.

⚡ Quick Answer

Three ways to write one: function add(a,b){ return a+b } (declaration), const add = function(a,b){...} (expression), and const add = (a,b) => a+b (arrow — the modern default for short functions).

§The three forms

forms.js
// 1. Declaration — hoisted, callable before its line
function greet(name) {
  return `Hello, ${name}!`;
}

// 2. Expression — a function stored in a variable
const shout = function (name) {
  return name.toUpperCase() + "!";
};

// 3. Arrow — concise; single expression returns implicitly
const add = (a, b) => a + b;
const square = n => n * n;        // one param: no parens needed
const hello = () => "hi";          // no params: empty parens

§Default & rest parameters

params.js
function power(base, exp = 2) {      // default value
  return base ** exp;
}
power(5);        // 25
power(5, 3);     // 125

function sum(...nums) {              // rest: gather all args
  return nums.reduce((t, n) => t + n, 0);
}
sum(1, 2, 3, 4);  // 10

§Callbacks — functions as arguments

callbacks.js
function repeat(times, action) {
  for (let i = 0; i < times; i++) action(i);
}
repeat(3, i => console.log(`round ${i}`));

// you use callbacks constantly with arrays:
const nums = [1, 2, 3, 4];
const doubled = nums.map(n => n * 2);      // [2,4,6,8]
button.addEventListener("click", () => {
  console.log("clicked!");                  // and with events
});

§Arrow functions & this

Arrows don't get their own this — they inherit it from the surrounding code. That's usually what you want in callbacks, and exactly what you don't want in object methods.

this.js
const timer = {
  seconds: 0,
  start() {                         // normal method: own this
    setInterval(() => {
      this.seconds++;               // arrow inherits timer's this ✓
    }, 1000);
  }
};

const bad = {
  name: "Ada",
  greet: () => `Hi ${this.name}`   // ✗ arrow method: this is NOT bad
};

§Common Mistakes to Avoid

🚨 Watch out for these
  • Calling vs referencingonClick(handle()) calls it immediately — pass the reference: onClick(handle)
  • Arrow functions as object methods — arrows inherit outer this, so this.name inside one won't be the object — use method shorthand
  • Forgetting return in bracesn => { n * 2 } returns undefined — braces need an explicit return

§Frequently Asked Questions

When should I use an arrow function?

For short callbacks and anywhere you want to keep the surrounding this — array methods, event handlers inside classes, promises. Use normal functions/method shorthand for object methods.

What is a callback function?

A function passed as an argument to be run later — the pattern behind array methods, event listeners and asynchronous code.

What is a higher-order function?

A function that takes or returns other functions — map, filter and addEventListener are all higher-order.

Are function declarations hoisted?

Yes — you can call a declaration before its line in the file. Expressions and arrows are not callable until their assignment runs.

🎓 Want to master JavaScript properly?This reference covers one topic — the full free course takes you from zero to real projects, with quizzes and a certificate.
Start the Free JavaScript Course →

§Related JavaScript Topics

← Data TypesArrays →