JavaScript Arrow Functions

In the previous lesson, we learned how to store a function in a variable as a function expression. Now letโ€™s learn a shorter, more modern way to write those functions: arrow functions.

๐Ÿน What is an Arrow Function?

An arrow function is a compact way to write a function expression. Instead of the function keyword, we use an arrow => (an equals sign followed by a greater-than sign) between the parameters and the body.

Here is a regular function expression and the same thing written as an arrow function:

arrow-functions.js
// Regular function expression
const add = function (a, b) {
return a + b;
};
// Arrow function
const add = (a, b) => {
return a + b;
};
console.log(add(5, 3)); // 8

Letโ€™s walk through what each part does:

  • The first add uses the function keyword, takes parameters a and b, and returns their sum.
  • The second add does the same job, but drops the function keyword and places => after the parameter list (a, b).
  • console.log(add(5, 3)) calls the function with 5 and 3, so it prints 8.

Both versions do exactly the same job. We drop the function keyword, and we place => after the parentheses that hold the parameters.

โœ‚๏ธ Implicit Return for One-Liners

When the function body is a single expression that returns a value, we can make it even shorter. We remove the curly braces and the return keyword, and JavaScript returns the result automatically. This is called an implicit return.

arrow-functions.js
// With curly braces and return
const add = (a, b) => {
return a + b;
};
// Implicit return: no braces, no return keyword
const add = (a, b) => a + b;
console.log(add(5, 3)); // 8

Here is how the short form maps onto the long one:

  • The first add keeps the curly braces { } and writes return a + b explicitly.
  • The second add removes the braces and the return, leaving just a + b after the arrow, so that value is returned automatically.
  • console.log(add(5, 3)) calls either version and prints 8.

The value a + b is returned without us writing return. This short form is one of the main reasons arrow functions are so popular.

When to use implicit return

Use the implicit return only when the body is a single expression. The moment you need more than one line, switch to curly braces and write return yourself.

๐ŸŽฏ Single Parameter, No Parentheses

When an arrow function takes exactly one parameter, we can leave out the parentheses around it.

arrow-functions.js
// With parentheses
const double = (n) => n * 2;
// Single parameter, parentheses omitted
const double = n => n * 2;
console.log(double(10)); // 20

Letโ€™s see what changed between the two lines:

  • The first double wraps its single parameter n in parentheses: (n).
  • The second double drops those parentheses and writes n on its own, which is allowed because there is exactly one parameter.
  • console.log(double(10)) passes 10 in for n, so it returns 10 * 2 and prints 20.

This rule applies only to a single parameter. With zero parameters or more than one, the parentheses are required.

arrow-functions.js
const greet = () => "Hello!"; // No parameters: parentheses required
const sum = (a, b) => a + b; // Two parameters: parentheses required

These two lines show when parentheses are mandatory:

  • greet takes no parameters, so we still need an empty pair of parentheses () before the arrow.
  • sum takes two parameters, so they must be wrapped in (a, b).

๐Ÿ“Š Regular Function vs Arrow Syntax

This table compares the same function written in different ways so the pattern is easy to see.

Style Syntax
Regular function expression const add = function (a, b) { return a + b; }
Arrow with body const add = (a, b) => { return a + b; }
Arrow with implicit return const add = (a, b) => a + b
Arrow with one parameter const double = n => n * 2
Arrow with no parameters const greet = () => "Hello!"

๐Ÿงฐ A Practical Example

Here is a small, real example. We have a function that calculates the final price after a discount.

arrow-functions.js
const getFinalPrice = (price, discount) => price - price * discount;
console.log(getFinalPrice(100, 0.2)); // 80
console.log(getFinalPrice(50, 0.1)); // 45

Letโ€™s trace what this code does:

  • getFinalPrice takes two parameters, price and discount, and uses an implicit return for price - price * discount.
  • price * discount is the discount amount, and subtracting it from price gives the final price.
  • The first call uses 100 and 0.2, so it subtracts 20 and prints 80.
  • The second call uses 50 and 0.1, so it subtracts 5 and prints 45.

The function takes a price and a discount rate, and returns the price minus the discount. The whole thing fits on one readable line.

Arrow functions show up everywhere

Arrow functions are extremely common with array methods like map, filter, and forEach, which we will cover in the next topics. Their short syntax keeps that code clean and easy to read.

๐Ÿ” A Note on this

Arrow functions also handle the this keyword differently from regular functions. An arrow function does not get its own this; it uses the this from the surrounding code instead. We will not go deep here, but it is good to know this difference exists for later lessons on objects.

Keep it light for now

For most everyday functions, the difference in this does not affect you. Just remember that arrow functions and regular functions are not identical in every situation.

โš ๏ธ Common Mistakes to Avoid

Mistake Problem Solution
Adding curly braces but forgetting return (a, b) => { a + b } returns undefined Add return, or drop the braces for implicit return
Wrong parentheses for parameters a, b => a + b is invalid for two parameters Wrap multiple parameters in ( )
Overusing arrows for complex bodies Long logic crammed into one line gets hard to read Use braces and clear lines when the body grows

Here is the most common slip in code, side by side:

arrow-functions.js
// โŒ Wrong: braces but no return, so it gives undefined
const add = (a, b) => {
a + b;
};
// โœ… Correct: implicit return with no braces
const add = (a, b) => a + b;

Letโ€™s compare the two versions:

  • The first add keeps the curly braces but only writes a + b; without return, so the function calculates the sum and then throws it away, returning undefined.
  • The second add removes the braces, so a + b becomes an implicit return and the sum actually comes back.

๐Ÿ”ง Try It Yourself!

  1. Write an arrow function multiply that takes two numbers and returns their product using an implicit return.
  2. Write an arrow function square with a single parameter and omit the parentheses around it.
  3. Write an arrow function sayHi that takes no parameters and returns the string "Hi!".
  4. Take a regular function expression you wrote before and rewrite it as a short arrow function.

๐Ÿงฉ What Youโ€™ve Learned

  • โœ… Arrow functions are a shorter way to write function expressions using =>
  • โœ… A single-expression body can use an implicit return with no braces and no return
  • โœ… A single parameter lets us drop the parentheses around it
  • โœ… Zero or multiple parameters still need parentheses
  • โœ… Arrow functions are common in array methods and treat this differently

๐Ÿš€ Whatโ€™s Next?

Now that we can write compact functions, letโ€™s learn how to pass functions into other functions. Letโ€™s continue to Callback Functions.

Share & Connect