Arrow functions — the modern short way
1 min read
A shorter way to write functions
Modern JavaScript has a shorter style called an arrow function. It does the same job, with less typing.
// old way
function add(a, b) {
return a + b;
}
// arrow way
const add = (a, b) => a + b;
Read the arrow => as the word "gives". So (a, b) => a + b means "a and b give back a + b".
Why do people love arrows?
Because for small functions they are clean and easy to read. When the body is a single line, you can even skip the { } and the word return — JavaScript returns that line for you automatically.
const square = (n) => n * n;
console.log(square(4)); // 16
With just one parameter, you may even drop the brackets:
const square = n => n * n;
When should I use which?
- Tiny one-line helpers → arrow function.
- Bigger blocks → either works; pick whichever reads clearly.
You will see arrow functions everywhere in real code (especially with arrays, coming up next), so it is good to get comfortable with them now.
Same idea, shorter clothes.
function add(a, b) { return a + b }and(a, b) => a + bdo the exact same thing.