Category(s)
Topic(s)
Arrow functions are a concise way to write functions in JavaScript, introduced in ES6. They use the =>
syntax and do not require the function keyword.
Syntax
const add = (a, b) => a + b;
console.log(add(5, 3)); // Output: 8
Single Parameter (No Parentheses Needed)
const square = x => x * x;
console.log(square(4)); // Output: 16
No Parameters (Requires Empty Parentheses)
const greet = () => "Hello, world!";
console.log(greet()); // Output: Hello, world!
Multiline Function (Using {}
and return
)
const multiply = (a, b) => {
let result = a * b;
return result;
};
console.log(multiply(4, 5)); // Output: 20
Features of Arrow Functions
- Shorter syntax
- No this binding (inherits from surrounding scope)
- Implicit return for single expressions
- Useful for callbacks and array methods