Magento 2 Experten — Hyvä Theme, Tailwind CSS & SEO aus einer Hand ›

Arrow Functions in JavaScript

Arrow Functions in JavaScript

~11 Min. Lesezeit Zuletzt aktualisiert am August 8, 2026

Arrow functions have been the most compact way to write functions in JavaScript since 2015 – and the most common in modern code. This chapter covers the syntax variants and one key behavioral difference from regular functions.

Basic syntax

// Function expression:
const doubleA = function (value) {
  return value * 2;
};

// Arrow function, equivalent:
const doubleB = (value) => {
  return value * 2;
};

console.log(doubleA(21), doubleB(21)); // 42 42

Shorthand forms

Arrow functions allow several shortcuts, depending on the number of parameters and body length:

// A single parameter: parentheses optional
const double = value => value * 2;

// Exactly ONE expression as the body: braces and 'return' are dropped (implicit return)
const isIncome = amount => amount > 0;

// No parameters: empty parentheses REQUIRED
const currentYear = () => 2026;

// Multiple parameters: parentheses REQUIRED
const add = (a, b) => a + b;

Achtung: To implicitly return an OBJECT LITERAL, it must be wrapped in parentheses – otherwise JavaScript interprets the curly braces as a function body, not an object: const makeTransaction = amount => ({ amount, type: 'expense' }); without the parentheses would cause a syntax error.

The key difference: arrow functions have no own this

Regular functions get a NEW this on every call, depending on HOW they were called (details in chapter 17). Arrow functions, on the other hand, have NO own this – they inherit this from their SURROUNDING context ("lexical this"). The following example demonstrates the difference directly on the budget app:

const account = {
  balance: 1500,
  transactions: [100, -50, 200],

  // Regular function as a method: 'this' correctly points to 'account'
  showBalanceRegular: function () {
    console.log(this.balance); // 1500 - 'this' is 'account'
  },

  // Arrow function as a method: 'this' does NOT point to 'account'!
  showBalanceArrow: () => {
    console.log(this.balance); // undefined - 'this' comes from the SURROUNDING context, not 'account'
  },
};

account.showBalanceRegular(); // 1500
account.showBalanceArrow();   // undefined

Achtung: So NEVER use an arrow function as a direct object METHOD when it needs this. Arrow functions are ideal for callbacks INSIDE a method (see below) – there, inheriting this from the surrounding method is exactly the desired behavior.

Arrow functions as callbacks: where they shine

const amounts = [2400, -850, -120, -60];

const expenses = amounts.filter(amount => amount < 0);
console.log(expenses); // [-850, -120, -60]

More on filter() and other array methods typically paired with arrow functions in chapter 11.

Another limitation: no own arguments object

Regular functions have access to an automatic arguments object (all passed arguments, regardless of declared parameters). Arrow functions do NOT have this – the modern replacement is rest parameters (...args) from chapter 8 anyway, which work in BOTH function forms.

Tipp: Rule of thumb: arrow functions as the DEFAULT for anything short and for callbacks. Use regular function syntax only where this should be determined dynamically by the caller (e.g. real object methods) or where an own arguments object is needed.