#1 India's Top IT Training Institute
×

About Uncodemy

Know more about who we are and what we stand for.

About Us

About Us

Discover our mission to equip individuals with essential IT skills and industry expertise.

Read More →
UnCodeMy Gallery

UnCodeMy Gallery

Explore highlights from our IT training sessions and success stories.

Read More →
Mentors

Mentors

Meet the industry experts who train and guide you through your course.

Read More →
Free Resources

Free Resources

Free tutorials, interview questions, tools and calculators to help you learn.

Explore →
×

Free Resources

Learn, practice and plan your career — bilkul free.

Tutorials

Tutorials

Step-by-step written tutorials to learn any technology at your own pace.

Read More →
Free Counselling

Free Counselling

Book a free video counselling session and pick the right course for you.

Read More →
Online Compiler

Online Compiler

Write, run and test your code online — no setup required.

Read More →
JavaScript Fundamentals

Arrow functions

Arrow functions are a shorter way to write function expressions using the => syntax. Beyond saving keystrokes, they behave differently from regular functions in one important way: they do not have their own this, so they use the this of the surrounding code. That makes them ideal for callbacks, array methods and small helpers, and a poor fit for object methods and constructors.

From function expression to arrow

// Regular function expression
const square = function (n) {
  return n * n;
};

// Arrow with a block body
const squareA = (n) => {
  return n * n;
};

// Arrow with an implicit return (expression body)
const squareB = (n) => n * n;

// No parameters, or several parameters, need parentheses
const now = () => Date.now();
const add = (a, b) => a + b;

// Returning an object literal needs wrapping parentheses
const makeUser = (name) => ({ name, active: true });

const prices = [120, 80, 250];
const withTax = prices.map((p) => p * 1.18);

With a single parameter you may drop the parentheses (n => n * n), though many style guides keep them for consistency.

How this works inside an arrow

A regular function gets its this from how it is called. An arrow function captures this from where it is defined, and nothing can change it, not even call, apply or bind. This solves an old problem with callbacks inside methods.

class Timer {
  constructor() {
    this.seconds = 0;
  }

  start() {
    // Arrow keeps "this" pointing at the Timer instance
    setInterval(() => {
      this.seconds += 1;
      console.log(this.seconds);
    }, 1000);
  }
}

new Timer().start();

If the callback were a regular function, this inside it would not be the timer, and this.seconds would be NaN or throw in strict mode. Older code solved this with const self = this or .bind(this).

Other differences from regular functions

  • No arguments object. Use rest parameters instead: (...args) => args.length.
  • Cannot be used with new; calling new on an arrow throws a TypeError.
  • No prototype property.
  • Not hoisted like function declarations; an arrow stored in const cannot be used before that line runs.

Where arrows cause bugs

The classic mistake is using an arrow as an object method. Suppose an object user has a name property and a method written as greetArrow: () => `Hi ${this.name}`. Calling user.greetArrow() does not print the name, because the arrow took this from the code around the object literal, not from user. Writing the method with shorthand syntax, greet() { return `Hi ${this.name}`; }, fixes it, since a normal method gets this from the object before the dot.

The same problem appears with DOM event listeners where you expect this to be the clicked element. Inside an arrow listener, use event.currentTarget instead.

A second slip is the object-literal return. () => { name: 'x' } returns undefined, because the braces are read as a function body and name: as a label. Wrap the object in parentheses.

When to choose which

Use an arrow forUse a regular function or method for
Array callbacks like map and filterObject and class methods that use this
Timers and promise handlers inside methodsConstructors
Short one-line helpersFunctions that need arguments or hoisting

Readability is the last consideration. Very long arrow chains, such as an arrow returning an arrow returning an arrow, are valid but hard to scan. If a callback grows beyond a few lines, give it a name with a regular declaration or a named const so stack traces and code reviews are clearer.

What to remember

Arrows are concise and take this from their surroundings. Use them for callbacks, wrap returned objects in parentheses, and avoid them for methods and constructors.

Ready to master real-world web development?

Learn HTML, CSS3, Bootstrap and JavaScript hands-on with mentor-led sessions.

Explore Course