Skip to main content

Command Palette

Search for a command to run...

Arrow Functions in JavaScript: A Simpler Way to Write Functions

Updated
5 min read
Arrow Functions in JavaScript: A Simpler Way to Write Functions

Modern JavaScript introduced arrow functions in ECMAScript 2015 to reduce boilerplate and make functions shorter and easier to read. Arrow functions use the => syntax and provide a concise way to write function expressions.

This article explains arrow functions step-by-step with simple examples.

1. What Arrow Functions Are

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

It performs the same job as a normal function but with less code and cleaner syntax.

Example

Normal function:

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

Arrow function:

const greet = (name) => {
  return "Hello " + name;
};

Both functions do the same thing — return a greeting message.

Arrow functions are commonly used in:

  • array methods (map, filter, reduce)

  • callbacks

  • modern JavaScript codebases


2. How Arrow Functions Reduce Boilerplate

Traditional functions require several keywords:

  • function

  • return

  • curly braces {}

Arrow functions remove unnecessary syntax.

Example Transformation

Normal Function

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

Arrow Function

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

Here we removed:

  • function

  • return

  • { }

This makes the code shorter and easier to read.


3. Basic Arrow Function Syntax

General syntax:

const functionName = (parameters) => {
  // function body
  return value;
};

Example:

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

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

Syntax Breakdown

(parameters) => { function body }

Diagram idea:

(parameters)   =>   { code }
     │          │        │
     │          │        └ function body
     │          └ arrow syntax
     └ function parameters

4. Arrow Functions with One Parameter

If the function has only one parameter, parentheses are optional.

Normal form:

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

Shorter version:

const square = num => num * num;

Example:

console.log(square(5)); // 25

5. Arrow Functions with Multiple Parameters

When a function has two or more parameters, parentheses are required.

Example:

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

console.log(add(5, 7)); // 12

Another example:

const greet = (name, age) => {
  return `Hello \({name}, age \){age}`;
};

6. Implicit Return vs Explicit Return

Arrow functions support two types of return behavior.


Explicit Return

When using curly braces {}, you must use return.

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

Implicit Return

If the function contains only one expression, you can remove {} and return.

const multiply = (a, b) => a * b;

Example:

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

Arrow functions automatically return the expression value in this case.


7. Basic Difference Between Arrow Function and Normal Function

Feature Normal Function Arrow Function
Syntax Uses function keyword Uses =>
Code length Longer Shorter
Return Needs return Can use implicit return
Usage Traditional style Modern JavaScript
Definition Can be a function declaration Always function expression

Example comparison:

Normal function:

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

Arrow function:

const square = num => num * num;

8. Using Arrow Functions with map()

Arrow functions are very common in array operations.

Example:

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

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

console.log(squares);

Output:

[1, 4, 9, 16]

This makes functional programming patterns much cleaner.


9. Practice Assignment

Try these exercises.

1️⃣ Convert Normal Function to Arrow Function

Normal function:

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

Rewrite using arrow function.


2️⃣ Even or Odd Checker

Create an arrow function that checks whether a number is even or odd.

Example:

const isEven = num => num % 2 === 0;

3️⃣ Use Arrow Function in map()

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

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

console.log(doubled);

Final Thoughts

Arrow functions are now a core part of modern JavaScript because they:

  • Reduce boilerplate code

  • improve readability

  • work perfectly with array methods and callbacks

For beginners, focus on:

  • simple syntax

  • implicit return

  • converting normal functions into arrow functions

Advanced topics like this Behavior can be explored later.