JavaScript Arrow Functions Explained for Beginners
What is an arrow function in JavaScript? Learn the => syntax step by step with simple examples, plain English and a quick practice quiz. Start here.
What is an arrow function in JavaScript? Learn the => syntax step by step with simple examples, plain English and a quick practice quiz. Start here.
What is an arrow function in JavaScript? An arrow function is a shorter way to write a function. Instead of the word function, you use a small arrow made of an equals sign and a greater-than sign (=>). This guide explains arrow functions for beginners, step by step, with small examples you can copy and run.
You do not need any experience. We define every new word the first time we use it. If you are new to functions, read our guide to JavaScript functions first, then come back here.
A function is a block of code with a name that you can run whenever you want. You give it some input, it does some work, and it can give back a result. Here is a normal function that adds two numbers.
function add(a, b) {
return a + b;
}
console.log(add(2, 3)); // 5Line 1 creates a function called add. The words a and b are parameters, which are names for the input values. Line 2 uses return to send back the answer. Line 5 calls the function (runs it) with 2 and 3 and prints 5.
Here is the same function written as an arrow function. We store it in a variable, which is a named box that holds a value.
const add = (a, b) => {
return a + b;
};
console.log(add(2, 3)); // 5const add creates a variable named add. The part (a, b) lists the parameters. The arrow => says 'here comes the body'. The curly braces hold the code to run. The result is the same as before: 5.
If your function only has one line that returns a value, you can drop the curly braces and the word return. JavaScript returns the value for you. This is called an implicit return (a return you do not have to write).
const add = (a, b) => a + b;
console.log(add(2, 3)); // 5Everything after the arrow is the value that comes back. This one line does exactly what the four-line version did. It is the main reason people love arrow functions.
When there is only one parameter, you can leave out the round brackets.
const double = n => n * 2;
console.log(double(4)); // 8Here n is the only parameter, so no brackets are needed. The function multiplies n by 2 and returns it. If you have zero parameters, you must write empty brackets: () => 'Hello'.
| Situation | Normal function | Arrow function |
|---|---|---|
| Two parameters | function add(a, b) { return a + b; } | (a, b) => a + b |
| One parameter | function double(n) { return n * 2; } | n => n * 2 |
| No parameters | function hi() { return 'Hi'; } | () => 'Hi' |
| Several lines | function f(x) { ... } | x => { ... } |
Arrow functions are most useful when you pass a small function into another function. Array methods like map and filter do this all the time. An array is a list of values.
const numbers = [1, 2, 3, 4, 5];
const doubled = numbers.map(n => n * 2);
const evens = numbers.filter(n => n % 2 === 0);
console.log(doubled); // [2, 4, 6, 8, 10]
console.log(evens); // [2, 4]map runs the arrow function on every item and builds a new list from the results. filter keeps only the items where the arrow function returns true. The symbol % gives the remainder after division, so n % 2 === 0 means 'n is even'. Without arrow functions, this would take many more lines.
You will also see arrow functions when you react to clicks on a web page. An event listener is code that waits for something to happen, like a button click.
const button = document.querySelector('button');
button.addEventListener('click', () => {
console.log('Button clicked!');
});The first line finds the button on the page. addEventListener takes two things: the event name ('click') and a function to run. The arrow function has no parameters, so we write (). It runs each time the button is clicked.
An object is a group of named values, written with curly braces. If you try to return an object with an implicit return, JavaScript thinks the curly braces are the function body. Wrap the object in round brackets to fix it.
// Wrong: returns undefined
const bad = () => { name: 'Sam' };
// Right: wrap the object in ( )
const good = () => ({ name: 'Sam' });
console.log(good()); // { name: 'Sam' }In bad, the braces are read as a code block, so nothing is returned. In good, the round brackets tell JavaScript 'this is a value, not a block'.
Arrow functions handle the word this differently from normal functions. The word this refers to the object that owns the code. Arrow functions do not get their own this. For beginners, a simple rule works well:
Want to go back to the basics? Learn how functions work, step by step.
Read: JavaScript Functions for BeginnersReady to practise with array methods that use arrow functions?
Read: map() and filter() ExplainedAnswers: 1) const square = x => x * x; 2) [1, 2, 3].map(n => n * 10); 3) [5, 12, 8, 20].filter(n => n > 10).
An arrow function is a shorter way to write a function using the => symbol. It was added to JavaScript in 2015. It does the same job as a normal function in most everyday cases.
The syntax is shorter, and arrow functions can return a value without the word return when the body is one expression. The biggest technical difference is that arrow functions do not have their own this. Normal functions do.
Yes. Write empty round brackets before the arrow, like () => 'Hello'. The brackets are required when there are zero parameters.
Only if the body has more than one statement or you want to write return yourself. For a single expression, you can leave out the braces and the return word.
Learn normal functions first so you understand the idea. Then use arrow functions for short callbacks like map and filter. Both are correct and you will see both in real projects.
Want help building a website or web app with clean JavaScript? Talk to the Fepiq team.
Contact FepiqOccasional, no-fluff notes on shipping modern software — startups, automation, Laravel, Shopify and more. No spam, unsubscribe anytime.
Keep reading
Learn what PHP cookies are and how to set, read and delete them with setcookie(). Simple examples and line-by-line explanations for absolute beginners.
What is useRef in React? Learn what a ref is, how it differs from useState, and how to focus an input, with short copy-paste examples for beginners.
Let's build something
Book a free discovery call. We'll listen, ask sharp questions, and send you a proposal within 3 business days.