Skip to main content

Command Palette

Search for a command to run...

Understanding Arrow Functions in JavaScript

Updated
3 min readView as Markdown

When learning JavaScript, one thing you quickly notice is that functions are used everywhere. But writing traditional functions sometimes feels a bit long for simple tasks.

This is where arrow functions come in. They provide a shorter and cleaner way to write functions, especially for small operations.

Arrow functions were introduced in ES6 and are now widely used in modern JavaScript.

What Are Arrow Functions?

An arrow function is simply a shorter way to write a function.

Instead of writing the full function keyword every time, arrow functions allow us to write the same logic using a more compact syntax.

Let’s look at a simple example.

Normal function:

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

Arrow function version:

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

Both functions do exactly the same thing. The arrow function just reduces some of the boilerplate.

Basic Arrow Function Syntax

The general syntax of an arrow function looks like this:

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

For example:

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

Here:

  • (a, b) are the parameters

  • => is the arrow syntax

  • The code inside {} is the function body

Arrow Functions with One Parameter

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

Example:

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

This function returns the square of a number.

Arrow Functions with Multiple Parameters

When there are multiple parameters, parentheses are required.

Example:

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

Here we pass two values, and the function returns their product.

Implicit Return vs Explicit Return

Arrow functions can sometimes return values without writing the return keyword.

Explicit Return

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

Implicit Return

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

In the second example:

  • No curly braces

  • No return keyword

JavaScript automatically returns the value.

Implicit return works best for small expressions.

Arrow Functions Inside Arrays

Arrow functions are very commonly used with array methods like map().

Example:

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

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

console.log(squares);

Output:

[1, 4, 9, 16]

Here, the arrow function runs for each number in the array and returns its square.

Basic Difference Between Arrow Function and Normal Function

At a beginner level, the main difference is syntax and readability.

Normal functions use the function keyword, while arrow functions use =>.

Example comparison:

Normal function:

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

Arrow function:

const double = num => num * 2;

Arrow functions make the code shorter and are commonly used in modern JavaScript.

Conclusion

Arrow functions provide a cleaner and shorter way to write functions in JavaScript. They help reduce repetitive syntax and make code easier to read, especially when working with arrays or small utility functions.

As you write more JavaScript, you’ll start using arrow functions naturally in many places.