An arrow function is a shorter way to write a function in JavaScript. It was added in ES6 (2015) and is now used everywhere: in array methods, event handlers, React components and Node.js code.
This page shows the syntax step by step, the one-line return shortcut, and the few simple ways arrow functions differ from normal functions. Every example below was run in Node.js and shows its real output. You can also run them in your browser's console (press F12, then open the Console tab) or in our online HTML, CSS and JS editor.
Arrow function syntax: from normal to arrow
Here is the same function written three ways:
// 1. Normal function declaration
function add(a, b) {
return a + b;
}
// 2. Arrow function with a body
const addArrow = (a, b) => {
return a + b;
};
// 3. Arrow function with a one-line return
const addShort = (a, b) => a + b;
console.log(add(2, 3), addArrow(2, 3), addShort(2, 3));
Output:
5 5 5
To turn a normal function into an arrow function:
- Remove the word
function. - Store the function in a variable, usually with
const. - Put
=>(the "arrow") between the parameters and the body. - If the body is just
return something;, drop the braces and thereturn.
The syntax rules in one table
| Situation | Syntax | Example |
|---|---|---|
| No parameters | Empty brackets are required | const greet = () => "Hello!"; |
| One parameter | Brackets are optional | const square = n => n * n; |
| Two or more parameters | Brackets are required | const add = (a, b) => a + b; |
| One expression | No braces, value returned automatically | const double = x => x * 2; |
| Several statements | Braces and return needed |
const f = x => { const y = x * 2; return y + 1; }; |
| Returning an object | Wrap the object in ( ) |
const make = name => ({ name }); |
const square = n => n * n;
const greet = () => "Hello!";
console.log(square(4));
console.log(greet());
Output:
16
Hello!
Many teams always keep the brackets, even with one parameter, as in (n) => n * n, because it looks consistent. Code formatters such as Prettier do this by default.
One-line returns (implicit return)
When an arrow function has no curly braces, the value of the expression after => is returned automatically. This is called an implicit return.
const isAdult = age => age >= 18; // returns true or false
const fullName = (first, last) => first + " " + last;
If you add curly braces, it becomes a normal function body, and you must write return yourself:
const isAdultWrong = age => { age >= 18; }; // returns undefined
const isAdultRight = age => { return age >= 18; };
Common mistake: Adding braces to a one-line arrow function and forgetting
return. The function quietly returnsundefined, and nothing throws an error to warn you.
Returning an object
Curly braces after => are always read as a function body. So to return an object, wrap it in parentheses:
const makeStudent = (name, marks) => ({ name, marks });
console.log(makeStudent("Ravi", 82));
const broken = (name) => { name: name };
console.log(broken("Ravi"));
Output:
{ name: 'Ravi', marks: 82 }
undefined
In broken, JavaScript sees a function body containing a label called name, not an object, so nothing is returned.
Where arrow functions shine: callbacks
A callback is a function you pass to another function. Arrow functions keep callbacks short and readable, especially with array methods:
const marks = [45, 82, 67, 91, 38];
const passed = marks.filter(m => m >= 40);
const withGrace = passed.map(m => m + 5);
const total = withGrace.reduce((sum, m) => sum + m, 0);
console.log(passed);
console.log(withGrace);
console.log(total);
Output:
[ 45, 82, 67, 91 ]
[ 50, 87, 72, 96 ]
305
Compare the filter line with the older style:
const passedOld = marks.filter(function (m) {
return m >= 40;
});
Same result, three times the code. Our JavaScript array methods guide has many more examples of map, filter and reduce. Arrow functions are also the usual way to write event handlers, as you will see in JavaScript DOM manipulation:
button.addEventListener("click", () => {
console.log("Button clicked");
});
Arrow vs normal function: the simple differences
| Point | Normal function | Arrow function |
|---|---|---|
| Syntax | function name(a) { ... } |
const name = (a) => ... |
| Implicit return | No | Yes, for one expression |
Own this |
Yes, set by how it is called | No, uses this from the surrounding code |
arguments object |
Yes | No (use rest parameters ...args) |
Can be used with new |
Yes | No, throws a TypeError |
| Hoisting | Function declarations can be called before the line they are written on | Stored in const, so they cannot be used before that line |
| Good for | Object methods, constructors | Callbacks, short helpers |
Hoisting
console.log(sayHi()); // works: "Hi"
function sayHi() { return "Hi"; }
console.log(sayHello()); // ReferenceError
const sayHello = () => "Hello";
A function declaration is available in its whole scope. An arrow function stored in const exists only after that line runs, so the second call throws a ReferenceError.
arguments and rest parameters
Normal functions have a built-in arguments object. Arrow functions do not. Use a rest parameter instead, which works in both:
const sumAll = (...nums) => nums.reduce((s, n) => s + n, 0);
console.log(sumAll(1, 2, 3, 4)); // 10
this in arrow functions
This is the difference that matters most, and the one interviewers ask about.
- A normal function gets its own
this, decided by how it is called. Called asstudent.regular(),thisisstudent. - An arrow function has no
thisof its own. It uses thethisof the code where it was written.
Problem: arrow function as an object method
const student = {
name: "Priya",
regular: function () {
return this === student;
},
arrow: () => {
return this === student;
},
};
console.log(student.regular());
console.log(student.arrow());
Output:
true
false
The arrow function was written at the top level of the file, not inside a function, so its this comes from there, not from student. Writing this.name inside arrow would not give you "Priya".
Rule: Do not use an arrow function as an object method if the method needs
this. Use the method shorthand instead:regular() { return this.name; }.
Solution: arrow function inside a method
Arrows are perfect inside a method, because they borrow the method's this:
const teacher = {
name: "Anil",
subjects: ["Java", "SQL"],
showAll() {
this.subjects.forEach((subject) => {
console.log(this.name + " teaches " + subject);
});
},
};
teacher.showAll();
Output:
Anil teaches Java
Anil teaches SQL
Inside showAll, this is teacher. The arrow callback has no this of its own, so it uses teacher too. Before arrow functions, developers wrote const self = this; or used .bind(this) to get the same effect.
The same idea works with timers:
const timer = {
seconds: 0,
start() {
const id = setInterval(() => {
this.seconds++;
console.log("Seconds: " + this.seconds);
if (this.seconds === 3) clearInterval(id);
}, 1000);
},
};
timer.start();
Output (one line per second):
Seconds: 1
Seconds: 2
Seconds: 3
If you replaced the arrow with function () { ... }, this inside the callback would no longer be timer, and the counter would not work.
When to use which
Use an arrow function for:
- Callbacks to
map,filter,reduce,forEachandsort. - Event listeners and timers inside methods or classes.
- Small helper functions, such as
const toUpper = s => s.toUpperCase();. - React function components and hooks, where arrows are the common style. See our React tutorial.
Use a normal function or method shorthand for:
- Object methods that use
this. - Constructor functions called with
new(or use aclass). - Functions that you want to call before they appear in the file.
Practice tasks
- Rewrite
function cube(n) { return n * n * n; }as a one-line arrow function. - Write an arrow function that takes a name and returns an object
{ name, length }. - Use
filterandmapwith arrow functions to get the names of students who scored above 75 from an array of objects. - Create an object with a
countproperty and astart()method that usessetIntervalwith an arrow function to count to 5.
Quick revision
- Arrow syntax:
(params) => expressionor(params) => { statements }. - One expression without braces returns automatically.
- Wrap returned objects in parentheses.
- Arrows have no own
this, noarguments, and cannot be used withnew. - Great for callbacks; avoid them as object methods that need
this.
To test yourself, try the JavaScript ES6 interview questions, where arrow functions and this come up often.
Frequently Asked Questions
What is an arrow function in JavaScript?
When can I skip the return keyword?
How do I return an object from an arrow function?
What is the difference between an arrow function and a normal function?
How does this work in arrow functions?
Should I always use arrow functions?
Want to Build Your Career in Java Full Stack with AI?
Join CodeBegun and train with working industry engineers — Explore the Java Full Stack program

