Skip to main content

Command Palette

Search for a command to run...

Arrow Functions : A Simpler Way to Write JS Function

All About Javascript Functions, ES6, Arrow Functions

Updated
7 min read
Arrow Functions : A Simpler Way to Write JS Function

Have you ever looked at your JavaScript code and wished you could write fewer lines? Do you find yourself typing function(){} repeatedly? If so, get ready to meet your new best friend: Arrow Functions!

Introduced in ES6 (ECMAScript 2015), arrow functions are a concise and modern way to write functions in JavaScript. They not only save you precious keystrokes but also make your code cleaner and easier to read. Let's dive in!

What Are Arrow Functions?

Before arrow functions existed, every function in JavaScript had to be written using the function keyword. It worked fine, but it was verbose — especially for small, simple operations like adding two numbers or greeting a user.

Arrow functions were introduced in ES6 (2015) as a shorter, cleaner way to write the same thing. Instead of the function keyword, you use a "fat arrow" => symbol, which gives them their name.

Arrow functions let you write the same logic with less code. They're not a replacement for every function — but for most everyday cases, they make your code much more readable.

Here's the big picture comparison before we dive in:

//Traditional way to define function
function greet(name) {
  return "Hello, " + name;
}
// With Arrow Functions (The New Way)
const greet = (name) => "Hello, " + name;

Same result. Half the lines. That's the promise of arrow functions.

Basic Arrow Function Syntax

Let's break down the anatomy of an arrow function piece by piece.

And here's the full transformation from a normal function to an arrow function:

The steps are simple: remove the function keyword, assign the function to a const variable, and add => between the parameters and the body.

Arrow Functions with One Parameter

When your function has exactly one parameter, you can drop the parentheses around it. This makes the syntax even more concise.

With parenthesis

const double = (n) => n * 2;

double(5);  // 10

Without parenthesis same result

const double = n => n * 2;

double(5);  // 10

Both versions do the same thing. Most developers keep the parentheses for consistency, but dropping them for single-parameter functions is a perfectly valid modern style.

const square  = n    => n * n;
const greet   = name => "Hey, " + name + "!";
const isAdult = age  => age >= 18;

console.log(square(4));          // 16
console.log(greet("Alice"));    // "Hey, Alice!"
console.log(isAdult(20));        // true

Arrow Functions with Multiple Parameters

When you have two or more parameters, parentheses are required. No exceptions.

const add      = (a, b)      => a + b;
const multiply = (a, b)      => a * b;
const greetFull = (first, last) => "Hello, " + first + " " + last;

add(3, 7);              // 10
multiply(4, 5);         // 20
greetFull("John", "Doe"); // "Hello, John Doe"

0 params → use () |  1 param → parenthesis optional  |  2+ params → parenthesis required

Zero parameters example

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

sayHello();  // "Hello, World!"

Implicit Return vs Explicit Return

This is one of the most powerful features of arrow functions — and the source of the most confusion. Let's clear it up.

Explicit Return

When you use curly braces { } for the function body, you must write return explicitly — just like a normal function

Explicit return (with curly braces)

const add = (a, b) => {
  return a + b;  // ← "return" is required here
};

add(3, 4);  // 7

Implicit Return

When you skip the curly braces and write the expression directly after =>, JavaScript automatically returns it. No return keyword needed. This is called an implicit return.

Implicit return (no curly braces)

const add = (a, b) => a + b;  // ← value is returned automatically

add(3, 4);  // 7

Let's see both side by side with a few examples:

Explicit Return Implicit Return
const sq = n => { return n * n; }; const greet = n => { return "Hi, " + n; }; const sq = n => n * n;

const greet = n => "Hi, " + n; |

Forgetting return when using curly braces. Without return, the function returns undefined silently.

// ❌ WRONG — returns undefined!
const add = (a, b) => {
  a + b;  // no return keyword
};

// ✅ CORRECT
const add = (a, b) => {
  return a + b;
};

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

Arrow vs Normal Functions

Arrow functions look different — but there are also some behavioral differences worth knowing.

Normal Function Arrow Function
Uses thefunctionkeyword Uses the=>fat arrow syntax
Has its ownthiscontext Inheritsthisfrom its parent scope
Can be used as a constructor withnew Cannot be used as a constructor
Has anargumentsobject Noargumentsobject
Can be declared before being defined (hoisting) Not hoisted — must be defined before use

For most everyday tasks — callbacks, transformations, short utilities — arrow functions are the modern, preferred choice. The this difference only matters in specific cases like object methods, which you'll explore as you advance.

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

// Arrow function — same behavior
const square = n => n * n;

// Both work the same way:
square(6);  // 36

You'll also see arrow functions shine inside array methods like map(), filter(), and reduce() — they make those callbacks incredibly clean to read:

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

// Double every number
const doubled = numbers.map(n => n * 2);
// [2, 4, 6, 8, 10]

// Keep only even numbers
const evens = numbers.filter(n => n % 2 === 0);
// [2, 4]

Some Practicle Examples

1. Normal Function – Square of a Number

function square(num) {
  return num * num;
}

2. Rewrite Using Arrow Function

const square = num => num * num;

3. Arrow Function – Even or Odd

const checkEvenOdd = num =>
  num % 2 === 0 ? "Even" : "Odd";

checkEvenOdd(4); // "Even"
checkEvenOdd(7); // "Odd"

4. Arrow Function Inside map()

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

const squares = numbers.map(num => num * num);

console.log(squares);
// [1, 4, 9, 16]

See how clean it looks inside map()?

Arrow functions are heavily used with:

  • map()

  • filter()

  • reduce()

  • forEach()

Conclusion

Arrow functions are a powerful addition to the JavaScript language. They offer a cleaner, more concise way to write functions, making your code more readable and maintaining a modern JavaScript style. While the difference in this binding is important to understand as you grow your JavaScript skills, for many common tasks, arrow functions simply provide a better, more efficient way to write your code. So, next time you're about to write function(), try using an arrow instead!