Skip to main content

Command Palette

Search for a command to run...

Arrow Functions in JavaScript: A Simpler Way to Write Functions

Master the modern, clean syntax of JavaScript Arrow Functions and learn how to write less boilerplate code

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

In our previous guide of the JavaScript Decoded series, we unlocked the powerhouse of JavaScript: Functions. We learned how to organize our logic using both Function Declarations and Function Expressions.

But what if I told you there was an even faster, cleaner, and more modern way to write functions?

Welcome to the world of Arrow Functions (=>).

Introduced in ES6 (a major JavaScript update in 2015), arrow functions changed the way developers write code. They allow us to strip away the boilerplate and write functions that are short, readable, and elegant.

In this guide, you will learn:

  • What arrow functions are and why developers love them

  • The basic syntax for writing them

  • How to handle zero, one, or multiple parameters

  • The magic of "Implicit Return" vs "Explicit Return"

  • The basic differences between arrow functions and normal functions

Let's clean up our code! 🚀


What Are Arrow Functions?

At their core, Arrow Functions are just a shorter syntax for writing Function Expressions.

They don't replace traditional functions entirely, but they act as a fantastic shortcut for writing small, concise logic. Think of them as the "shorthand" version of a function.

The Problem: Too Much Boilerplate

When writing a traditional function expression, you have to type the word function, the curly braces {}, and the return keyword.

If your function only does one simple thing, that feels like a lot of extra typing:

// The old, long way (Function Expression)
const sayHi = function(name) {
  return "Hi, " + name;
};

Watch what happens when we convert this exact same logic into an arrow function:

// The modern, clean way (Arrow Function)
const sayHi = (name) => "Hi, " + name;

Boom! We shrunk three lines of code into a single, highly readable line. Let's break down how we got there.


Arrow Function Syntax Breakdown

Creating an arrow function is like taking a normal function and aggressively trimming the fat.

Here is what happens during the transformation:

  1. Remove the function keyword completely.

  2. Add a "fat arrow" => just after the parentheses ().

Basic Syntax:

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

console.log(addNumbers(5, 10)); // Output: 15

This is the standard syntax for an arrow function with multiple lines of code inside the body. It works exactly like the functions you already know!


Dealing with Parameters

Arrow functions are so flexible that their syntax actually changes slightly depending on how many parameters (arguments) you are passing in.

1. Multiple Parameters (Requires Parentheses)

If your function takes two or more parameters, you must wrap them in parentheses ().

const subtract = (x, y) => {
  return x - y;
};

2. Exactly One Parameter (Parentheses are Optional!)

If your function only requires a single parameter, JavaScript lets you drop the parentheses altogether to make the code even cleaner.

// With parentheses (totally fine)
const double = (num) => {
  return num * 2;
};

// Without parentheses (cleaner and preferred by many developers)
const doubleCleaner = num => {
  return num * 2;
};

3. Zero Parameters (Requires Empty Parentheses)

If your function doesn't take any parameters at all, you must use an empty set of parentheses () to let JavaScript know it's a function.

const greetWorld = () => {
  console.log("Hello, World!");
};

Implicit Return vs. Explicit Return

This is where Arrow Functions truly shine.

In a normal function, if you want to output a value, you must use the return keyword explicitly. Arrow functions, however, can return values automatically under the right conditions. This is called an Implicit Return.

Explicit Return (The standard way)

If your arrow function uses curly braces {}, you must use the return keyword. This is an explicit return. Use this if your function spans multiple lines of logic.

const multiply = (x, y) => {
  const result = x * y;
  return result; // We MUST use 'return' because of the { }
};

Implicit Return (The secret superpower)

If your function contains only one single expression (one line of logic that evaluates to a value), you can:

  1. Delete the curly braces {}

  2. Delete the return keyword

JavaScript will implicitly assume that whatever is on the right side of the arrow => is what you want to return.

// No curly braces, no 'return' keyword!
const multiplyShorthand = (x, y) => x * y;

console.log(multiplyShorthand(3, 4)); // Output: 12

💡 Rule of Thumb:

  • If you use curly braces {}, you need to write return.

  • If you omit the curly braces, the return happens automatically!


Arrow Functions vs Normal Functions

Since Arrow Functions look so great, should you stop using normal functions entirely? No! While they are fantastic, they are not 100% identical to normal functions under the hood.

Here are the basic differences you should know as a beginner:

  1. Syntax: Arrow functions are shorter and omit the function keyword.

  2. Hoisting: Because Arrow Functions are stored in variables (like const), they are not hoisted. You must define them before you call them down the page. (Normal Function Declarations are hoisted).

  3. The this Keyword: Normal functions behave differently depending on how they are called. Arrow functions are much simpler—they inherit their context from the surrounding code. (Don't worry if this sounds confusing right now; we will dedicate an entire future blog post exclusively to the this keyword!)


Why Developers Love Arrow Functions

You will see Arrow Functions everywhere in modern JavaScript, especially when working with Arrays. Because they are so short, they are perfect for passing quick, one-off logic into other functions.

(Sneak peek of what we will cover in the upcoming Arrays post!)

const names = ["Debesh", "Ankit", "Priya"];

// Look how clean this is! No 'function' or 'return' needed.
const excitedNames = names.map(name => name + "!");

console.log(excitedNames); 
// Output: ["Debesh!", "Ankit!", "Priya!"]

Your Turn: Assignment Time! 💻

Ready to practice? Open your code editor and try solving these challenges:

  1. Write a normal function expression named calculateSquare that takes a number, multiplies it by itself, and returns the result.

  2. Rewrite that exact same function into an Arrow Function with an Implicit Return (no curly braces or return keyword).

  3. Create an Arrow Function called isEven that takes a number n. If the number is even, return true. If it's odd, return false. (Hint: use the modulo operator %!)

  4. Extra Credit: Create an array of numbers [1, 2, 3, 4]. Use the .map() method along with an arrow function to double every number in the array.


What's Next?

Now that you've mastered both traditional functions and arrow functions, you have the tools needed to write clean, reusable logic. Next up, we will tackle repeating actions!

  • Loops and Iterationfor, while, and executing logic repeatedly

  • Arrays — Storing lists of data

  • Array Methodsmap, filter, and reduce

Happy coding! 🎉

JavaScript Decoded

Part 3 of 6

JavaScript Decoded is a step-by-step blog series designed for beginners and self-taught developers who want to truly understand JavaScript from the ground up. Each post covers one core concept — variables, control flow, functions, arrays, objects, and more — with practical code examples, beginner-friendly explanations, and tips you can apply right away. No fluff, just clarity.

Up next

Function Declaration vs Function Expression: What’s the Difference?

Learn the fundamentals of functions in JavaScript, including declarations, expressions, syntax, and how hoisting works