Arrow Functions
Arrow functions are a shorter way to write function expressions.
They were introduced in ES6 and are used everywhere in modern JavaScript.
Regular function expression:
const add = function (a, b) {
return a + b;
};Arrow function:
const add = (a, b) => {
return a + b;
};Shorter arrow function:
const add = (a, b) => a + b;Basic Syntax
const functionName = (parameters) => {
// function body
};Example:
const greet = (name) => {
return `Hello, ${name}`;
};
console.log(greet("Alice")); // Hello, AliceThe arrow => separates the parameters from the function body.
Single Expression Return
If the function body has only one expression, you can omit the braces and return.
const double = (number) => number * 2;
console.log(double(5)); // 10This is called an implicit return.
These two functions behave the same:
const double = (number) => {
return number * 2;
};const double = (number) => number * 2;Use the shorter version when it stays readable.
One Parameter
If there is exactly one parameter, parentheses are optional.
const square = number => number * number;
console.log(square(4)); // 16Many teams still prefer parentheses for consistency:
const square = (number) => number * number;Both are valid.
Zero Parameters
If there are no parameters, you must use parentheses.
const getMessage = () => "Hello!";
console.log(getMessage()); // Hello!Multiple Parameters
If there are multiple parameters, parentheses are required.
const multiply = (a, b) => a * b;
console.log(multiply(3, 4)); // 12Returning Objects
When returning an object literal implicitly, wrap the object in parentheses.
Wrong:
const createUser = (name) => { name: name };JavaScript treats {} as a function body, not an object.
Correct:
const createUser = (name) => ({ name: name });
console.log(createUser("Alice")); // { name: "Alice" }With property shorthand:
const createUser = (name) => ({ name });Arrow Functions With Array Methods
Arrow functions are commonly used with array methods.
const numbers = [1, 2, 3];
const doubled = numbers.map((number) => number * 2);
console.log(doubled); // [2, 4, 6]With filter:
const scores = [90, 45, 70, 30];
const passing = scores.filter((score) => score >= 60);
console.log(passing); // [90, 70]With reduce:
const numbers = [1, 2, 3, 4];
const total = numbers.reduce((sum, number) => sum + number, 0);
console.log(total); // 10Arrow functions are especially useful for short callbacks.
Multi-Line Arrow Functions
Use braces when the function needs multiple statements.
const formatUser = (user) => {
const name = user.name.trim();
const role = user.role.toUpperCase();
return `${name} (${role})`;
};When you use braces, you must write return if you want to return a value.
const double = (number) => {
number * 2;
};
console.log(double(5)); // undefinedCorrect:
const double = (number) => {
return number * 2;
};Arrow Functions and this
Arrow functions do not have their own this.
They use this from the surrounding scope.
You studied this in the previous module.
This is why arrow functions are not a good choice for object methods that need this.
Avoid:
const user = {
name: "Alice",
greet: () => {
return `Hello, ${this.name}`;
},
};Prefer:
const user = {
name: "Alice",
greet() {
return `Hello, ${this.name}`;
},
};Arrow functions are excellent for callbacks.
They are not a replacement for every function.
Arrow Functions Are Not Hoisted Like Function Declarations
Arrow functions are usually stored in variables.
const greet = () => "Hello";This follows the rules of const.
You cannot call it before the declaration.
greet(); // ReferenceError
const greet = () => "Hello";Function declarations behave differently:
greet(); // Hello
function greet() {
return "Hello";
}When to Use Arrow Functions
Arrow functions are great for:
- short callbacks
- array methods
- simple transformations
- functions that do not need their own
this
Example:
const names = users.map((user) => user.name);Regular functions are often better for:
- object methods that use
this - constructor functions
- functions where hoisting improves readability
- longer named functions
Best Practices
Use concise arrow functions for simple expressions:
const ids = users.map((user) => user.id);Use braces and return for multi-step logic:
const labels = users.map((user) => {
const status = user.active ? "Active" : "Inactive";
return `${user.name}: ${status}`;
});Use method shorthand for object methods that need this.
Do not force arrow functions into every situation.
Common Mistakes
Mistake 1: Forgetting return With Braces
const doubled = numbers.map((number) => {
number * 2;
});This returns undefined for each item.
Correct:
const doubled = numbers.map((number) => {
return number * 2;
});Or:
const doubled = numbers.map((number) => number * 2);Mistake 2: Returning an Object Without Parentheses
const users = names.map((name) => { name });Correct:
const users = names.map((name) => ({ name }));Mistake 3: Using Arrow Functions for Methods That Need this
const user = {
name: "Alice",
greet: () => this.name,
};Use method shorthand instead.
Quick Check
What does this return?
const add = (a, b) => a + b;
add(2, 3);It returns:
5What does this return?
const createUser = (name) => ({ name });
createUser("Alice");It returns:
{ name: "Alice" }Summary
Arrow functions provide a shorter syntax for function expressions.
- Use
=>to create an arrow function. - Single-expression arrow functions can return implicitly.
- Braces require an explicit
return. - Wrap object literals in parentheses when returning them implicitly.
- Arrow functions are common with array methods.
- Arrow functions do not have their own
this. - Avoid arrow functions for object methods that need
this.