DEV Community

Cover image for Arrow Functions: The Modern Chef’s Secret to Clean Code
Subhrangsu Bera
Subhrangsu Bera

Posted on

Arrow Functions: The Modern Chef’s Secret to Clean Code

If you’ve been writing JavaScript for a while, you’ve probably seen the => symbol popping up everywhere.It looks like an arrow, it’s called an Arrow Function, and it’s about to make your code look a lot sleeker.

Think of it as a shorthand recipe. Instead of writing out a full, formal instruction card, you’re just jotting down the essentials on a sticky note.

It does the same job, but with much less "boilerplate" (the extra words we usually have to type).

1. The Transformation: From Normal to Arrow

Let’s look at a simple recipe: Squaring a Number.

The Traditional Recipe (Function Expression):

const square = function(n) {
    return n * n;
};
Enter fullscreen mode Exit fullscreen mode

The Modern Arrow Recipe:

const square = (n) => {
    return n * n;
};
Enter fullscreen mode Exit fullscreen mode

The Transformation: From Normal to Arrow

We removed the word function and added a => after the parameters. It’s already looking cleaner!

2. One Parameter? No Problem!

The "Chef's shorthand" gets even faster. If your recipe only needs one ingredient (one parameter), you can even drop the parentheses ().

// Look, no parentheses around 'n'!
const square = n => {
    return n * n;
};
Enter fullscreen mode Exit fullscreen mode

3. Multiple Ingredients (Parameters)

If your recipe needs more than one ingredient, you just put the parentheses back.

const multiply = (a, b) => {
    return a * b;
};

console.log(multiply(5, 4)); // Output: 20
Enter fullscreen mode Exit fullscreen mode

4. The Magic of "Implicit Return"

This is the coolest part of Arrow Functions. If your recipe is so simple that it only has one line of code, you can throw away the curly braces {} and the return keyword entirely. This is called an Implicit Return.

Traditional (Explicit):

const add = (a, b) => { return a + b; };
Enter fullscreen mode Exit fullscreen mode

Arrow Style (Implicit):

const add = (a, b) => a + b;
Enter fullscreen mode Exit fullscreen mode

JavaScript looks at that single line and says, "I get it, you want to return the result of a + b."

Arrow function syntax breakdown

5. Arrow Functions in the Real World: Array Methods

Remember our "Data Train" (Arrays)? Arrow functions are the perfect companions for methods like map().

The Goal:

Check an array of numbers and return if they are "Even" or "Odd."

const numbers = [1, 2, 3, 4];

const checkNumbers = numbers.map(num => num % 2 === 0 ? "Even" : "Odd");

console.log(checkNumbers); // ["Odd", "Even", "Odd", "Even"]
Enter fullscreen mode Exit fullscreen mode

Imagine how much space you’d waste writing function(num) { return ... } inside that map!

6. Key Differences (High Level)

While they look cool, there are a few things Arrow Functions can't do compared to the "Wall Poster" recipes we learned before:

Feature Normal Function Arrow Function
Syntax Verbose (Wordy) Concise (Short)
this keyword Has its own "context." Does NOT have its own this.
Hoisting Depends (Declaration = Yes). No. (Must be defined first).
Arguments Has an arguments object. No arguments object.

Chef’s Note:
For now, just remember: arrow functions are perfect for short logic and callbacks.


Summary Challenge: The Square-Off

Try this in your console.

Step 1 — Normal Function

function square(n) {
  return n * n;
}
Enter fullscreen mode Exit fullscreen mode

Step 2 — Arrow Function

const square = n => n * n;
Enter fullscreen mode Exit fullscreen mode

Step 3 — Call Both

console.log(square(4)); // 16
Enter fullscreen mode Exit fullscreen mode

Notice how much simpler the arrow version looks.


Final Thoughts

Arrow functions are a standard feature of modern JavaScript (ES6+).

They help make code:

  • shorter
  • easier to read
  • more expressive

Once you start using them, they quickly become your default way to write functions.

Mastering arrow functions doesn't just make you a faster coder — it also makes your code easier for other developers to understand.

Top comments (0)