Skip to main content

Command Palette

Search for a command to run...

Arrow Functions in JavaScript: A Simpler Way to Write Functions

Less typing, same power. Arrow functions are one of the best things ES6 added to JavaScript.

Updated
8 min read
C
Software developer passionate about building scalable web applications with React and backend technologies. I enjoy solving problems, building projects, and sharing my learning with the community.

The Problem: Functions Have a Lot of Boilerplate

Here's a simple function that doubles a number:

function double(n) {
  return n * 2;
}

It works fine. But notice how much of that is just ceremony — function, curly braces, return. For a single-line operation, that feels heavy.

Arrow functions let you write the same thing like this:

const double = n => n * 2;

Same result. One line. No function, no return, no curly braces. That's the core idea.


Basic Arrow Function Syntax

Let's convert a normal function step by step:

// Normal function
function greet() {
  return "Hello, World!";
}

// Step 1: Remove the `function` keyword
//         Assign it to a const
// Step 2: Add => after the parentheses
// Step 3: Remove {} and return for single expressions

const greet = () => "Hello, World!";

console.log(greet()); // "Hello, World!"

The full arrow function anatomy:

const functionName = (parameters) => expression
      ────────────   ────────────   ──────────
           │               │             │
        variable       inputs        what it
         name          go here       returns

Arrow Functions With No Parameters

Use empty parentheses () when there are no parameters:

const sayHello  = () => "Hello!";
const getDate   = () => new Date().toLocaleDateString();
const randomNum = () => Math.floor(Math.random() * 100);

console.log(sayHello());   // "Hello!"
console.log(getDate());    // "29/4/2026" (or whatever today is)
console.log(randomNum());  // some random number

Arrow Functions With One Parameter

When there's exactly one parameter, you can drop the parentheses:

// With parentheses — valid ✅
const double = (n) => n * 2;

// Without parentheses — also valid ✅ (and more common)
const double = n => n * 2;

const square    = n => n * n;
const isEven    = n => n % 2 === 0;
const greetUser = name => `Hello, ${name}!`;

console.log(square(5));        // 25
console.log(isEven(4));        // true
console.log(greetUser("Aarav")); // "Hello, Aarav!"

💡 Parentheses are optional for a single parameter — but some teams prefer always using them for consistency. Both styles are fine.


Arrow Functions With Multiple Parameters

With two or more parameters, parentheses are required:

const add      = (a, b) => a + b;
const multiply = (a, b) => a * b;
const fullName = (first, last) => `\({first} \){last}`;
const clamp    = (val, min, max) => Math.min(Math.max(val, min), max);

console.log(add(3, 5));               // 8
console.log(multiply(4, 6));          // 24
console.log(fullName("Aarav", "Shah")); // "Aarav Shah"
console.log(clamp(15, 0, 10));        // 10

Implicit Return vs Explicit Return

This is the part that confuses most beginners. Let's break it clearly.

Implicit Return

When your function body is a single expression, you can skip the curly braces and return — JavaScript returns it automatically.

const add = (a, b) => a + b;  // implicit return

Explicit Return

When you need multiple lines or complex logic, use curly braces — and then you must write return yourself.

const add = (a, b) => {
  const result = a + b;
  console.log(`Adding \({a} and \){b}`);
  return result;              // explicit return required
};

The rule is simple:

No curly braces  →  return is automatic (implicit)
With curly braces → you must write return (explicit)
// ✅ Implicit — one expression, no braces, auto-returned
const square = n => n * n;

// ✅ Explicit — multi-line, braces, manual return
const describe = n => {
  const sq = n * n;
  return `\({n} squared is \){sq}`;
};

// ❌ Common mistake — braces but no return
const broken = n => {
  n * n; // calculated, but never returned!
};

console.log(broken(5)); // undefined 😬

Returning an Object Literal

Returning an object implicitly needs extra parentheses — otherwise JavaScript thinks the {} is a function body:

// ❌ Broken — JS thinks {} is a function body
const makeUser = (name, age) => { name, age };

// ✅ Wrap the object in () to fix it
const makeUser = (name, age) => ({ name, age });

console.log(makeUser("Aarav", 25)); // { name: "Aarav", age: 25 }

Arrow Functions in the Real World

Where arrow functions truly shine is as inline callbacks — functions passed to other functions.

With map()

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

// Normal function
const doubled = numbers.map(function(n) {
  return n * 2;
});

// Arrow function — much cleaner
const doubled = numbers.map(n => n * 2);

console.log(doubled); // [2, 4, 6, 8, 10]

With filter()

const numbers = [1, 2, 3, 4, 5, 6, 7, 8];

const evens = numbers.filter(n => n % 2 === 0);
const odds  = numbers.filter(n => n % 2 !== 0);
const big   = numbers.filter(n => n > 5);

console.log(evens); // [2, 4, 6, 8]
console.log(odds);  // [1, 3, 5, 7]
console.log(big);   // [6, 7, 8]

With reduce()

const prices = [99, 249, 59, 399];

const total = prices.reduce((sum, price) => sum + price, 0);

console.log(total); // 806

Chaining them all

const orders = [120, 45, 300, 80, 500, 30];

const result = orders
  .filter(order => order >= 100)   // only big orders
  .map(order => order * 0.9)       // apply 10% discount
  .reduce((sum, order) => sum + order, 0); // total

console.log(result); // 828

One chain, zero function keywords, reads almost like English.


Arrow Function vs Normal Function: Key Differences

Both define functions — but they behave differently in a few important ways.

For now, the most practical differences to remember:

1. Arrow functions are not hoisted

// Normal function — hoisted, can call before definition
sayHi(); // ✅ works

function sayHi() {
  console.log("Hi!");
}

// Arrow function — not hoisted
greet(); // ❌ ReferenceError

const greet = () => console.log("Hello!");

2. Arrow functions can't be used as constructors

const Person = (name) => {
  this.name = name;
};

const p = new Person("Aarav"); // ❌ TypeError: Person is not a constructor

Use a regular function or class when you need new.

3. No arguments object

function normal() {
  console.log(arguments); // ✅ [1, 2, 3]
}
normal(1, 2, 3);

const arrow = () => {
  console.log(arguments); // ❌ ReferenceError
};
arrow(1, 2, 3);

// Use rest parameters instead:
const arrow = (...args) => {
  console.log(args); // ✅ [1, 2, 3]
};

Assignment

1. Write a normal function, then convert to arrow:

// Normal
function square(n) {
  return n * n;
}

// Arrow
const square = n => n * n;

console.log(square(7)); // 49

2. Even or odd checker:

const isEven = n => n % 2 === 0 ? "Even" : "Odd";

console.log(isEven(4));  // "Even"
console.log(isEven(7));  // "Odd"

3. Arrow function inside map():

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

const squares  = numbers.map(n => n * n);
const evens    = numbers.filter(n => n % 2 === 0);
const total    = numbers.reduce((sum, n) => sum + n, 0);

console.log(squares); // [1, 4, 9, 16, 25]
console.log(evens);   // [2, 4]
console.log(total);   // 15

Bonus: Chain them

const result = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
  .filter(n => n % 2 === 0)   // keep evens: [2,4,6,8,10]
  .map(n => n * n)             // square them: [4,16,36,64,100]
  .reduce((sum, n) => sum + n, 0); // total: 220

console.log(result); // 220

Summary

Arrow functions aren't a replacement for regular functions — they're a more concise syntax best suited for short, simple operations and callbacks.

Here's the whole thing in one place:

// No params
const greet = () => "Hello!";

// One param (parens optional)
const double = n => n * 2;

// Multiple params (parens required)
const add = (a, b) => a + b;

// Multi-line (explicit return required)
const describe = (a, b) => {
  const sum = a + b;
  return `\({a} + \){b} = ${sum}`;
};

// Returning an object
const makeUser = (name, age) => ({ name, age });

Once you're comfortable with arrow functions, your code will look cleaner, feel more modern, and be much easier to read at a glance.


< Keep Coding />

More from this blog

Why Node.js is Perfect for Building Fast Web Applications

Every technology makes a bet. Node.js's bet was this: most web applications aren't slow because they do too much computation. They're slow because they spend most of their time waiting — waiting for a database to respond, waiting for a file to load, waiting for an external API to return. If you build a runtime optimised around that specific reality, you get something genuinely fast for the work most web apps actually do.

May 9, 202611 min read1
C

Chetan Chauhan | Tech Blog | chetan71

41 posts