Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / JavaScript

Arrow function syntax

Arrow functions use `=>`. The parameter list determines whether parentheses are required, and the body form determines whether a value is returned implicitly. The surrounding scope supplies `this`.

Version: ECMAScript 2024 and the current web platform; runtime support varies

Syntax

x => expression
() => expression
(a, b) => expression
({ amount }, factor = 2) => expression
(...args) => expression
(params) => { return expression; }
(params) => ({ key: value })
async x => expression
async (params) => { return await expression; }

Parameters and body

Arrow function syntax defines a function expression, not a function declaration. The => punctuator separates parameters from the body; it is not an operator. Omit parentheses only for a single parameter written as a plain identifier: x => x * 2. Use parentheses for no parameters, multiple parameters, or a default, destructured, or rest parameter such as ...args.

An expression body implicitly returns the expression's value. A { immediately after => starts a block of statements: use return to return a value. If execution reaches the end of the block, a non-async arrow returns undefined. To return an object literal without return, write () => ({ total: 99 }). In () => { total: 99; }, total: is a label, not an object key.

Call context and async

References to this, super, and new.target depend on the surrounding lexical context, where permitted. An arrow does not create an implicit arguments binding. If there is no local binding named arguments, references to it are resolved in the surrounding scope. In non-strict code, however, an arrow can declare a parameter named arguments, which provides a local binding. call, apply, and bind cannot change an arrow's this. Use a regular method when the function needs the object it was called on. Arrows cannot be generators, and invoking one with new throws a TypeError.

A line break is not allowed between the parameters and =>, but it can follow =>. In async (x) => x, do not put a line break between async and the start of the parameter list either. An async arrow returns a Promise and can use await in its body. Check syntax support in your target JavaScript runtime.

Examples

{
  const calculator = {
    offset: 3,
    makeFormatter() {
      return ({ amount }, factor = 2) => ({ total: amount * factor + this.offset });
    },
  };

  const format = calculator.makeFormatter();
  const noResult = () => { total: 99; };

  console.log(format({ amount: 4 }).total);
  console.log(format.call({ offset: 100 }, { amount: 4 }).total);
  console.log(String(noResult()));
}

Result

11
11
undefined

The first two lines are `11`: destructuring extracts `amount`, `factor` defaults to `2`, and `this.offset` stays `3` despite `call`. The last line is `undefined` because the `noResult` block has no `return`.

Related lessons

  • JavaScript · Lesson 20 · Advanced · Version: 4
  • JavaScript · Lesson 21 · Advanced · Version: 4
  • JavaScript · Lesson 22 · Advanced · Version: 4
  • JavaScript · Lesson 23 · Advanced · Version: 4
  • JavaScript · Lesson 24 · Advanced · Version: 4
  • JavaScript · Lesson 25 · Advanced · Version: 4
  • JavaScript · Lesson 27 · Advanced · Version: 4
  • JavaScript · Lesson 28 · Advanced · Version: 4
  • JavaScript · Lesson 29 · Advanced · Version: 4
  • JavaScript · Lesson 30 · Advanced · Version: 4
  • JavaScript · Lesson 31 · Advanced · Version: 4
  • JavaScript · Lesson 32 · Advanced · Version: 4
  • JavaScript · Lesson 33 · Advanced · Version: 4
  • JavaScript · Lesson 34 · Advanced · Version: 4
  • JavaScript · Lesson 35 · Advanced · Version: 4
  • JavaScript · Lesson 36 · Advanced · Version: 4
  • JavaScript · Lesson 37 · Advanced · Version: 4
  • JavaScript · Lesson 38 · Advanced · Version: 4
  • JavaScript · Lesson 39 · Advanced · Version: 4
  • JavaScript · Lesson 40 · Advanced · Version: 4
  • JavaScript · Lesson 41 · Advanced · Version: 4
  • JavaScript · Lesson 42 · Advanced · Version: 4
  • JavaScript · Lesson 43 · Advanced · Version: 4
  • JavaScript · Lesson 44 · Advanced · Version: 4
  • JavaScript · Lesson 45 · Advanced · Version: 4

Sources