JavaScript Arrow Functions
Table of Contents + โ
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:
// Regular function expressionconst add = function (a, b) { return a + b;};
// Arrow functionconst add = (a, b) => { return a + b;};
console.log(add(5, 3)); // 8Letโs walk through what each part does:
- The first
adduses thefunctionkeyword, takes parametersaandb, and returns their sum. - The second
adddoes the same job, but drops thefunctionkeyword and places=>after the parameter list(a, b). console.log(add(5, 3))calls the function with5and3, so it prints8.
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.
// With curly braces and returnconst add = (a, b) => { return a + b;};
// Implicit return: no braces, no return keywordconst add = (a, b) => a + b;
console.log(add(5, 3)); // 8Here is how the short form maps onto the long one:
- The first
addkeeps the curly braces{ }and writesreturn a + bexplicitly. - The second
addremoves the braces and thereturn, leaving justa + bafter the arrow, so that value is returned automatically. console.log(add(5, 3))calls either version and prints8.
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.
// With parenthesesconst double = (n) => n * 2;
// Single parameter, parentheses omittedconst double = n => n * 2;
console.log(double(10)); // 20Letโs see what changed between the two lines:
- The first
doublewraps its single parameternin parentheses:(n). - The second
doubledrops those parentheses and writesnon its own, which is allowed because there is exactly one parameter. console.log(double(10))passes10in forn, so it returns10 * 2and prints20.
This rule applies only to a single parameter. With zero parameters or more than one, the parentheses are required.
const greet = () => "Hello!"; // No parameters: parentheses requiredconst sum = (a, b) => a + b; // Two parameters: parentheses requiredThese two lines show when parentheses are mandatory:
greettakes no parameters, so we still need an empty pair of parentheses()before the arrow.sumtakes 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.
const getFinalPrice = (price, discount) => price - price * discount;
console.log(getFinalPrice(100, 0.2)); // 80console.log(getFinalPrice(50, 0.1)); // 45Letโs trace what this code does:
getFinalPricetakes two parameters,priceanddiscount, and uses an implicit return forprice - price * discount.price * discountis the discount amount, and subtracting it frompricegives the final price.- The first call uses
100and0.2, so it subtracts20and prints80. - The second call uses
50and0.1, so it subtracts5and prints45.
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:
// โ Wrong: braces but no return, so it gives undefinedconst add = (a, b) => { a + b;};
// โ
Correct: implicit return with no bracesconst add = (a, b) => a + b;Letโs compare the two versions:
- The first
addkeeps the curly braces but only writesa + b;withoutreturn, so the function calculates the sum and then throws it away, returningundefined. - The second
addremoves the braces, soa + bbecomes an implicit return and the sum actually comes back.
๐ง Try It Yourself!
- Write an arrow function
multiplythat takes two numbers and returns their product using an implicit return. - Write an arrow function
squarewith a single parameter and omit the parentheses around it. - Write an arrow function
sayHithat takes no parameters and returns the string"Hi!". - 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
thisdifferently
๐ 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.