⬅ Back

ARROW FUNCTIONS IN JAVASCRIPT

This note explains arrow functions in simple language.

You will learn:

  1. what an arrow function is
  2. how arrow function syntax works
  3. the difference between explicit return and implicit return
  4. why arrow functions do not have arguments
  5. how arrow functions are used in callbacks

Arrow functions are very common in modern JavaScript, especially in:

1. What is an arrow function?

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

It uses the arrow symbol:

=>

Arrow functions help reduce code, especially when the function is small.

Diagram 1. Main idea

Regular function expression
↓
more code

Arrow function
↓
shorter code

Explanation

Arrow functions do the same job as normal functions in many situations, but with shorter syntax.

2. Regular function vs arrow function

Regular function

function classicAdd(a, b, c) {
  return a + b + c;
}

Arrow function

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

Both functions do the same thing.

Diagram 2. Same logic, different syntax

classicAdd(a, b, c)
→ returns a + b + c

arrowAdd(a, b, c)
→ returns a + b + c

Explanation

The main difference is the syntax, not the result.

3. Important rule: arrow functions are function expressions

Arrow functions are always created like function expressions.

That means they are usually stored in a variable.

Example

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

Diagram 3. Arrow function structure

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

1. const add
2. parameters
3. =>
4. function body

Explanation

Arrow functions are not written like classic function declarations.

They are written as values assigned to variables.

4. Basic arrow syntax

Example

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

This means:

Diagram 4. Arrow syntax parts

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

parameters → (a, b, c)
arrow      → =>
body       → { return a + b + c; }

Explanation

This is the standard full form of an arrow function.

5. Arrow function with several parameters

If there are several parameters, use parentheses.

Example

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

Diagram 5. Several parameters

(a, b, c)
↓
three parameters
↓
parentheses are required

Explanation

When there is more than one parameter, parentheses must be used.

6. Arrow function with one parameter

If there is only one parameter, parentheses can be omitted.

Example

const add = a => {
  return a + 5;
};

This is also valid:

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

Diagram 6. One parameter

One parameter:
a => { ... }

Also valid:
(a) => { ... }

Explanation

When there is exactly one parameter, the shorter form without parentheses is allowed.

7. Arrow function with no parameters

If there are no parameters, empty parentheses are required.

Example

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

Diagram 7. No parameters

() => { ... }

No parameters
↓
empty parentheses are required

Explanation

You cannot remove the parentheses when there are zero parameters.

8. Two body styles of arrow functions

Arrow functions can be written in two main ways:

  1. with curly braces
  2. without curly braces

This changes how return works.

Diagram 8. Two body styles

Arrow function body
│
├─ with { }
│  └─ use explicit return
│
└─ without { }
   └─ use implicit return

Explanation

This is one of the most important things to understand about arrow functions.

9. Explicit return

If the arrow function uses curly braces {}, then you must write return if you want to return a value.

Example

const add = (a, b, c) => {
  console.log(a, b, c);
  return a + b + c;
};

Diagram 9. Explicit return

(a, b, c) => {
  console.log(a, b, c);
  return a + b + c;
}

Has { }
↓
must use return

Explanation

Because the body uses curly braces, JavaScript will not return the result automatically.

10. Why explicit return is useful

Use explicit return when the function does more than one thing.

For example:

Example

const add = (a, b, c) => {
  console.log("Adding numbers");
  return a + b + c;
};

Diagram 10. Multiple actions in function body

1. do some code
2. return result

Explanation

If you need multiple statements, use curly braces and explicit return.

11. Implicit return

If the arrow function does not use curly braces, JavaScript automatically returns the result of the expression.

This is called implicit return.

Example

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

Diagram 11. Implicit return

(a, b, c) => a + b + c

No { }
↓
result is returned automatically

Explanation

This is shorter and cleaner when the function only returns one expression.

12. Regular function to arrow with implicit return

Before

function classicAdd(a, b, c) {
  return a + b + c;
}

After

const arrowAdd = (a, b, c) => a + b + c;

Diagram 12. Shorter version

Before:
function + return + braces

After:
parameters => expression

Explanation

This is one of the biggest reasons arrow functions are popular.

13. When implicit return is a good choice

Use implicit return when:

Example

const double = number => number * 2;

Diagram 13. Best use of implicit return

Short function
One expression
No extra code
↓
implicit return is a good choice

Explanation

This keeps the code simple and readable.

14. When implicit return is not a good choice

If you need more than just returning one expression, use curly braces.

Example

const double = number => {
  console.log("Doubling number");
  return number * 2;
};

Diagram 14. Use braces for extra steps

Need logging?
Need extra statements?
Need several steps?
↓
use { } and return

Explanation

Implicit return is only for simple cases.

15. Arrow functions do not have arguments

A very important rule:

Arrow functions do not have their own local arguments variable.

Example

const add = (...args) => {
  console.log(args);
};

add(1, 2, 3); // [1, 2, 3]

Diagram 15. No local arguments

Arrow function
↓
no local arguments
↓
use rest parameter instead

Explanation

If you need all arguments in an arrow function, use:

...args

This collects them into an array.

16. Use rest instead of arguments

Example

const logArgs = (...args) => {
  console.log(args);
};

logArgs("a", "b", "c"); // ["a", "b", "c"]

Diagram 16. Rest in arrow functions

logArgs("a", "b", "c")
↓
...args collects all arguments
↓
args = ["a", "b", "c"]

Explanation

This is the normal modern way to collect all arguments in an arrow function.

17. Arrow functions as callbacks

Arrow functions are very convenient for callbacks because they are shorter.

This is especially useful in array methods.

Example with forEach()

const numbers = [5, 10, 15, 20, 25];

// Regular anonymous function
numbers.forEach(function (number, index) {
  console.log(`Index ${index}, value ${number}`);
});

// Arrow function
numbers.forEach((number, index) => {
  console.log(`Index ${index}, value ${number}`);
});

Diagram 17. Callback styles

Regular callback
→ longer

Arrow callback
→ shorter

Explanation

That is why arrow functions are very common in methods like:

18. Why arrow callbacks are popular

Arrow callbacks are popular because they:

Diagram 18. Why arrows are common in callbacks

Small callback
↓
arrow function
↓
shorter and cleaner

Explanation

When the callback is small, arrow syntax is often the best choice.

19. Arrow callback written separately

Sometimes the callback is bigger or reused in several places.

Then it is better to declare it separately.

Example

const numbers = [5, 10, 15, 20, 25];

const logMessage = (number, index) => {
  console.log(`Index ${index}, value ${number}`);
};

numbers.forEach(logMessage);

Diagram 19. Separate callback

Create function first
↓
store in variable
↓
pass by reference later

Explanation

This is useful when:

20. Short callback with implicit return

Arrow functions become especially short when used with implicit return.

Example

const double = number => number * 2;

This style is very common in modern JavaScript.

Diagram 20. Smallest useful arrow form

number => number * 2

one parameter
one expression
implicit return

Explanation

This is one of the cleanest arrow function forms.

21. Common beginner mistakes

Mistake 1. Forgetting that arrow functions are expressions

Wrong idea:

arrowAdd(a, b) => a + b

Correct:

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

Mistake 2. Forgetting return when using braces

Wrong:

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

This returns undefined.

Correct:

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

Mistake 3. Forgetting empty parentheses for no parameters

Wrong:

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

Correct:

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

Mistake 4. Thinking arrow functions have arguments

They do not. Use rest parameters instead.

Diagram 21. Common mistakes summary

1. Arrow functions are expressions
2. With { }, use return
3. With no parameters, use ()
4. No local arguments in arrow functions

Explanation

These are some of the most common beginner problems with arrow functions.

22. Practical examples

Example 1. One parameter

const addFive = number => number + 5;
console.log(addFive(10)); // 15

Example 2. No parameters

const sayHello = () => "Hello!";
console.log(sayHello()); // "Hello!"

Example 3. Several parameters

const multiply = (a, b, c) => a * b * c;
console.log(multiply(2, 3, 4)); // 24

Example 4. Callback with forEach()

const colors = ["red", "green", "blue"];

colors.forEach((color, index) => {
  console.log(index, color);
});

Example 5. Rest parameter in arrow function

const collect = (...args) => args;
console.log(collect(1, 2, 3)); // [1, 2, 3]

Diagram 22. Real uses of arrow functions

Arrow functions are useful for:
- short helpers
- callbacks
- array methods
- small reusable expressions

Explanation

This is why you will see them constantly in modern JavaScript.

23. Quick summary

Arrow function

A shorter way to write a function expression.

=>

The arrow symbol used in arrow functions.

Explicit return

When {} is used, write return yourself.

Implicit return

When {} is not used, the expression is returned automatically.

No arguments

Arrow functions do not have a local arguments variable.

Use rest

Use ...args if you need all arguments.

Great for callbacks

Arrow functions are especially convenient in array methods.

Diagram 23. Final map of arrow functions

Arrow functions
│
├─ shorter syntax
├─ function expression form
├─ one / many / zero parameters
├─ explicit return
├─ implicit return
├─ no arguments
├─ use rest instead
└─ very useful for callbacks

Explanation

This is the full beginner picture of arrow functions.

24. Revision block

1. Arrow functions use the => symbol
2. They are written as function expressions
3. One parameter can be written without parentheses
4. No parameters require empty parentheses
5. With curly braces, return must be written explicitly
6. Without curly braces, the result is returned automatically
7. Arrow functions do not have a local arguments variable
8. Use rest parameters to collect all arguments
9. Arrow functions are very useful for callbacks
10. They are often used in array methods

25. Final conclusion

Arrow functions are one of the most important modern JavaScript features.

If you understand:

then you already have a strong base for reading and writing modern JavaScript.

Arrow functions are used everywhere in full-stack development:

If you want, I can make the next note about implicit return with object literals or arrow functions and this in the same style.

⬅ Back