logo

Arrow Functions

Explain arrow function syntax and how lexical `this` differs from regular functions.

Short explanation

Arrow functions are a concise syntax for writing functions. They also capture the this value lexically from the surrounding scope, which makes them especially useful for callbacks.

Syntax example

const add = (a, b) => a + b;

const obj = {
  value: 42,
  getValue: () => this.value, // this is lexically inherited β€” often undefined in this context
};

How JS handles it internally

Arrow functions do not create their own this binding, arguments object, or prototype. Instead, they use the this from the enclosing scope. Because of these differences, they are not suitable as constructors.

FAQ

Q: Can arrow functions be used as constructors?

A: No β€” they do not have a [[Construct]] method and will throw when used with new.