Understanding Arrow Functions in JavaScript

When developers first start learning JavaScript, functions are usually written using the traditional function keyword. It works well, but as applications grow, developers often look for ways to make their code shorter, clearer, and easier to read.
This is where arrow functions come in. Introduced in ES6, arrow functions provide a modern and concise way to write functions in JavaScript.
To understand them better, let’s walk through a few small real-world scenarios where arrow functions make everyday coding easier.
Building a Simple Greeting Feature
Imagine you’re building a small website that greets users when they log in.
The first way many beginners write this function looks like this:
function greetUser(name) {
return "Hello " + name;
}
It works perfectly. If the user’s name is Sameer, the function returns:
Hello Sameer
But modern JavaScript allows us to write the same logic in a shorter way using an arrow function:
const greetUser = (name) => {
return "Hello " + name;
};
Here, the => symbol replaces the function keyword, making the syntax more compact.
Understanding the Basic Arrow Function Syntax
At its core, an arrow function follows this structure:
const functionName = (parameters) => {
};
For example, imagine a small calculator feature on a webpage that adds two numbers.
Arrow Function Syntax Breakdown
const add = (a, b) => a + b
│ │ │ │
│ │ │ └── Expression returned
│ │ └──────── Arrow operator
│ └──────────────── Function parameters
└────────────────────── Variable storing the function
const addNumbers = (a, b) => {
return a + b;
};
console.log(addNumbers(5, 3));
Output:
8
The arrow function receives two values and returns their sum, just like a normal function.
Arrow Functions with One Parameter
Now imagine you’re building a small math utility that calculates the square of a number.
Using a traditional function:
function square(num) {
return num * num;
}
Using an arrow function:
const square = num => {
return num * num;
};
console.log(square(6));
Output:
36
Because there is only one parameter, JavaScript allows us to remove the parentheses. This makes the code slightly cleaner.
Arrow Functions with Multiple Parameters
Now imagine a small shopping cart feature where you multiply the price of an item by the quantity.
For example:
Item price: 50
Quantity: 3
Total: 150
Using an arrow function:
const calculateTotal = (price, quantity) => {
return price * quantity;
};
console.log(calculateTotal(50, 3));
Output:
150
When there are multiple parameters, the parentheses are required.
Implicit Return vs Explicit Return
Arrow functions also introduce a powerful feature called implicit return, which can make very small functions even shorter.
Explicit Return
This version uses the return keyword just like traditional functions.
const multiply = (a, b) => {
return a * b;
};
Implicit Return
If the function contains only one expression, JavaScript can return the result automatically.
const multiply = (a, b) => a * b;
This version removes both the curly braces and the return keyword while producing the same result.
For example:
console.log(multiply(4, 5));
Output:
20
This pattern is extremely common in modern JavaScript.
A Real Example: Working with a List of Prices
Imagine you are building an online store and have a list of product prices:
let prices = [100, 200, 300];
Now suppose there is a discount campaign where every price should be reduced by 20%.
Using map() with an arrow function:
let discountedPrices = prices.map(price => price * 0.8);
console.log(discountedPrices);
Output:
[80, 160, 240]
The arrow function takes each price, applies the discount, and returns a new array. Because the function is short, the arrow syntax keeps the code very readable.
Normal Function vs Arrow Function
Let’s compare the two styles side by side.
Traditional function:
function greet(name) {
return "Hello " + name;
}
Arrow function:
const greet = name => "Hello " + name;
Key differences:
| Normal Function | Arrow Function |
|---|---|
Uses the function keyword |
Uses the => arrow operator |
| Requires more syntax | More concise and compact |
| Common in older JavaScript | Preferred in modern JavaScript |
Both functions work the same way, but the arrow function removes extra syntax, which is why it is widely used in modern JavaScript projects.
Arrow functions have become a core part of modern JavaScript because they allow developers to express logic in a concise and readable way. Whether you're writing small utility functions, transforming arrays, or handling callbacks, arrow functions help keep the code simple and expressive.






