Arrow Functions

Short syntax for writing functions.

Examples

Basic Arrow Function

Standard arrow function syntax.

const add = (a, b) => {
  return a + b;
};
console.log(add(5, 3)); // 8

Concise One-Liner

Implicit return when curly braces are omitted.

const multiply = (a, b) => a * b;
console.log(multiply(4, 5)); // 20

Single Parameter

Parentheses are optional for a single parameter.

const greet = name => "Hello " + name;
console.log(greet("Bob"));

This Binding

Arrow functions do not bind their own 'this'.

const obj = {
  value: 42,
  getValue: () => this.value // 'this' is not bound to obj
};
// In global scope, 'this.value' is likely undefined
console.log(obj.getValue());

Test Your Knowledge

JavaScript Quiz

No quiz available for this topic yet.