JavaScriptFunctionsbeginner

JavaScript Arrow Functions Explained

Arrow function syntax step by step, one-line returns, arrows with map and filter, and the simple differences from normal functions, including this.

Published
TL;DR – Quick Answer

An arrow function is a shorter way to write a function in JavaScript, added in ES6 (2015). Instead of function add(a, b) { return a + b; } you can write const add = (a, b) => a + b;. When the body is a single expression, you can drop the braces and the return keyword. Arrow functions do not have their own this; they use the this of the code around them. They also cannot be used with new and do not have an arguments object.

On This Page

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:

  1. Remove the word function.
  2. Store the function in a variable, usually with const.
  3. Put => (the "arrow") between the parameters and the body.
  4. If the body is just return something;, drop the braces and the return.

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 returns undefined, 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 as student.regular(), this is student.
  • An arrow function has no this of its own. It uses the this of 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, forEach and sort.
  • 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 a class).
  • Functions that you want to call before they appear in the file.

Practice tasks

  1. Rewrite function cube(n) { return n * n * n; } as a one-line arrow function.
  2. Write an arrow function that takes a name and returns an object { name, length }.
  3. Use filter and map with arrow functions to get the names of students who scored above 75 from an array of objects.
  4. Create an object with a count property and a start() method that uses setInterval with an arrow function to count to 5.

Quick revision

  • Arrow syntax: (params) => expression or (params) => { statements }.
  • One expression without braces returns automatically.
  • Wrap returned objects in parentheses.
  • Arrows have no own this, no arguments, and cannot be used with new.
  • 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?
It is a compact function syntax that uses => instead of the function keyword. For example, const square = n => n * n; creates a function that returns the square of n.
When can I skip the return keyword?
When the body is a single expression written without curly braces. The value of that expression is returned automatically. If you use curly braces, you must write return yourself.
How do I return an object from an arrow function?
Wrap the object in parentheses: const make = (name) => ({ name });. Without the parentheses, JavaScript reads the braces as a function body and returns undefined.
What is the difference between an arrow function and a normal function?
Arrow functions are shorter, do not have their own this or arguments, cannot be used as constructors with new, and are not hoisted like function declarations. Normal functions have all of these.
How does this work in arrow functions?
An arrow function does not create its own this. It uses this from the place where it was written. That makes arrows good for callbacks inside methods, but a poor choice for object methods themselves.
Should I always use arrow functions?
Use them for short callbacks and small helper functions. Use normal functions or method shorthand for object methods, constructors and functions that need their own this.

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

Apply for Demo Class →
Siva Prasad Galaba
Founder, CodeBegun · Staff Engineer

Founder of CodeBegun. 15+ years building Java systems at companies like Crunchyroll. Teaches Java, Spring Boot and system design the way the industry actually works, and mentors students through projects, mock interviews and placement preparation.

Technically reviewed by CodeBegun Technical TeamLast reviewed 5 December 2026 LinkedIn
Chat with us