Arrow Functions in JavaScript: Write Less, Mean More

Search for a command to run...

No comments yet. Be the first to comment.
Introduction If you've ever built something with vanilla JavaScript and had to update the UI frequently, you already know the pain. You call document.getElementById, change some text, update a class,
Introduction If you've ever installed and tried out Linux after seeing a cool hacking sequence in a sci-fi movie, you know that opening it feels like stepping into a strange city with many roads named

Introduction Most JavaScript confusion around this comes from one wrong assumption: that this depends on where a function is defined. It doesn't. It depends on who called the function. That one shift

Introduction JavaScript gives you objects and arrays out of the box, and for a long time those two were the default answer to almost every data storage problem. Objects for key-value pairs, arrays for

Introduction Most JavaScript codebases are full of code like this: const user = { name: "Sara", age: 28, city: "Mumbai" }; const name = user.name; const age = user.age; const city = user.city; Three

If you've been writing JavaScript for a while, you've almost certainly run into this kind of code:
function add(a, b) {
return a + b;
}
Nothing wrong with it. But modern JavaScript gives you a shorter way to write the same thing, and once you get used to it, going back feels like putting on a heavy coat. Arrow functions cut the boilerplate and make your code easier to scan at a glance.
Arrow functions are a shorter syntax for writing functions in JavaScript. They were introduced in ES6 (2015) and have become the default style in most modern codebases.
They're not a replacement for every function you'll ever write. But for simple operations, callbacks, and one-liners, they're cleaner.
Here's the same add function rewritten as an arrow function:
const add = (a, b) => {
return a + b;
};
Instead of the function keyword, you write the parameters in parentheses, then =>, then the function body. That => is the "arrow" in arrow function.
The structure is:
const functionName = (parameters) => {
// body
};
When your function takes exactly one parameter, you can drop the parentheses:
// Normal function
function greet(name) {
return "Hello, " + name;
}
// Arrow function
const greet = name => {
return "Hello, " + name;
};
Both do the same thing. The arrow version just skips the parens around name.
If there are zero parameters or two or more, the parentheses come back:
const sayHi = () => {
return "Hi!";
};
Two or more parameters always need parentheses:
// Normal function
function multiply(a, b) {
return a * b;
}
// Arrow function
const multiply = (a, b) => {
return a * b;
};
Same result, slightly less to type.
This is where arrow functions really start to pull ahead.
When your function body is a single expression, you can skip the curly braces and the return keyword. JavaScript will return the value automatically. This is called an implicit return.
// Explicit return (with curly braces)
const square = (n) => {
return n * n;
};
// Implicit return (no curly braces, no return keyword)
const square = (n) => n * n;
Both versions return n * n. The second one just states it directly.
A few more examples:
const double = n => n * 2;
const greet = name => "Hello, " + name;
const add = (a, b) => a + b;
Clean and readable. You can tell at a glance what each function does.
One thing to watch out for: if you're returning an object literal, wrap it in parentheses. Otherwise JavaScript reads the curly braces as a function body, not an object.
// This breaks
const getUser = () => { name: "Alex" };
// This works
const getUser = () => ({ name: "Alex" });
The outer parentheses tell JS "this is an expression, not a block."
The most obvious difference is syntax. But there's one behavioral difference worth knowing early: arrow functions don't have their own this.
You don't need to dig into this deeply right now. For simple tasks like math operations, string manipulation, and short callbacks, arrow functions work exactly the same as regular functions.
Here's a quick side-by-side:
// Normal function
function add(a, b) {
return a + b;
}
// Arrow function
const add = (a, b) => a + b;
// Normal function with no parameters
function sayHello() {
return "Hello!";
}
// Arrow function with no parameters
const sayHello = () => "Hello!";
// Normal function, one parameter
function double(n) {
return n * 2;
}
// Arrow function, one parameter
const double = n => n * 2;
The pattern is consistent. Strip function, move the parameters to the left of =>, and if it's a single expression, drop the braces and return.
Arrow functions work well for:
Short utility functions (add, double, greet)
Callbacks in array methods like map, filter, and reduce
Any place where you want concise, readable code
For longer functions, functions that need their own this, or constructors, regular functions are still the right call. Arrow functions are a tool, not a universal replacement.
But for the majority of functions you write day to day? The arrow syntax keeps things clean, reduces visual noise, and reads more like math than ceremony.
// Zero parameters
const sayHi = () => "Hi!";
// One parameter (parens optional)
const double = n => n * 2;
// Multiple parameters
const add = (a, b) => a + b;
// Multi-line body (needs curly braces and return)
const greet = (name) => {
const message = "Hello, " + name;
return message;
};
// Returning an object
const getUser = () => ({ name: "Alex", age: 30 });
Once the syntax clicks, you'll start seeing arrow functions everywhere in modern JavaScript code. And you'll probably start writing them that way too.