JavaScript Arrow Functions

Beginner
6 min read
Updated: Nov 2026
3 examples

Definition and Usage

An arrow function is a shorter way to write a function with =>. It does not have its own this, arguments, or prototype—it borrows this from the surrounding scope.

JavaScript Syntax

JavaScript
const double = (n) => n * 2;
const add = (a, b) => a + b;
const greet = (name) => {
  return "Hello, " + name;
};

console.log(double(4));
console.log(add(2, 3));
console.log(greet("Ada"));
Try it Editor

How It Works

  1. One expression. When the body is a single expression, you can omit braces and return—(n) => n * 2 returns the doubled value.
  2. Two parameters. (a, b) => a + b needs parentheses when there is more than one parameter.
  3. Block body. With braces you must write return yourself, just like a normal function body.

Examples

Example 1 — Map with an Arrow

Pass an arrow into map to transform every item.

JavaScript
const nums = [1, 2, 3];
const doubled = nums.map((n) => n * 2);
console.log(doubled.join(", "));
Try it Editor

How It Works

  1. Callback. map calls the arrow once per item with that item as n.
  2. New array. The results are collected into a new array: [2, 4, 6].

Example 2 — Filter Even Numbers

Keep only values that pass a short predicate.

JavaScript
const nums = [1, 2, 3, 4, 5, 6];
const evens = nums.filter((n) => n % 2 === 0);
console.log(evens.join(", "));
Try it Editor

How It Works

  1. Predicate. The arrow returns true only for even numbers.
  2. Result. filter builds a new array of the values that passed.

Example 3 — Lexical this

Arrows keep the outer this—useful inside methods and timers.

JavaScript
const counter = {
  value: 0,
  start() {
    const tick = () => {
      this.value += 1;
      console.log(this.value);
    };
    tick();
    tick();
  }
};
counter.start();
Try it Editor

How It Works

  1. Outer this. start is a method, so its this is counter.
  2. Arrow inherits. The arrow uses that same this, so this.value updates the object.

Frequently Asked Questions

A compact function written with =>. Example: const double = (n) => n * 2.

No. They use this from the enclosing scope (lexical this).

Avoid them as object methods when you need this to be the object, or as constructors—arrows cannot be used with new.

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

6 people found this page helpful