Skip to main content

Command Palette

Search for a command to run...

Arrow Functions in JavaScript: A Simpler Way to Write Functions

Published
4 min read
Arrow Functions in JavaScript: A Simpler Way to Write Functions

Modern JavaScript introduced arrow functions to make writing functions shorter, cleaner, and easier to read.

If you've already learned function declarations and function expressions, arrow functions are the next step.

Arrow functions help reduce boilerplate code, which means you can write the same logic with less syntax.

For example, consider this normal function:

function add(a, b) {
  return a + b;
}

Using an arrow function, we can write it like this:

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

Same logic, but written in a modern JavaScript style.

Let’s explore how arrow functions work.

What Are Arrow Functions?

An arrow function is a shorter way to write a function in JavaScript using the => syntax.

They were introduced in ES6 (ECMAScript 2015) and are widely used in modern JavaScript code.

General syntax:

const functionName = (parameters) => {
  // code
};

Example:

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

greet();

Output:

Hello!

Arrow functions are commonly used in:

  • array methods (map, filter, forEach)

  • callbacks

  • small utility functions

Basic Arrow Function Syntax

Let's convert a normal function into an arrow function.

Normal Function

function multiply(a, b) {
  return a * b;
}

Arrow Function

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

Both functions produce the same result.

Example usage:

console.log(multiply(3, 4));

Output:

12

Arrow functions mainly help reduce unnecessary syntax.

Arrow Functions with One Parameter

If the function has only one parameter, you can omit the parentheses.

Normal Function

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

Arrow Function

const square = num => {
  return num * num;
};

Example:

console.log(square(5));

Output:

25

Parentheses are optional when there is only one parameter.

Arrow Functions with Multiple Parameters

When a function has multiple parameters, parentheses are required.

Example:

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

console.log(add(10, 5));

Output:

15

Here:

  • (a, b) → parameters

  • => → arrow syntax

  • {} → function body

Implicit Return vs Explicit Return

Arrow functions provide a feature called implicit return, which allows you to return values without using the return keyword.

Explicit Return

Example:

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

Here we explicitly use the return keyword.

Implicit Return

If the function only contains one expression, we can simplify it.

const add = (a, b) => a + b;

This automatically returns the result.

Example:

console.log(add(4, 6));

Output:

10

This is one of the biggest advantages of arrow functions.

They make simple functions very compact and readable.


Arrow Functions with Simple Examples

Greeting Example

Normal function:

function greet(name) {
  return "Hello " + name;
}

Arrow function:

const greet = name => "Hello " + name;

console.log(greet("Alex"));

Output:

Hello Alex

Much shorter and easier to read.


Arrow Functions with Arrays

Arrow functions are very commonly used with array methods.

Example using map().

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

let doubled = numbers.map(num => num * 2);

console.log(doubled);

Output:

[2, 4, 6, 8]

Arrow functions make this code much cleaner.


Basic Difference Between Arrow Function and Normal Function

Feature Normal Function Arrow Function
Syntax Longer Shorter
Return Must use return Can use implicit return
Parameters Parentheses required Optional for single parameter
Style Traditional JavaScript Modern JavaScript

Arrow functions are often preferred because they improve readability and reduce boilerplate code.

However, normal functions are still useful in some cases.

For beginners, it’s important to understand both styles.


Quick Example Comparison

Normal function:

function subtract(a, b) {
  return a - b;
}

Arrow function:

const subtract = (a, b) => a - b;

Both produce the same result, but the arrow function is shorter.