HexLibraryHexLibrary
</> HexCodeSign inSign up
PythonJavaScriptTypeScriptCC++JavaPHPGoRustRubyBashSQLLuaPerlCOBOLPascalDelphiFortranDartSwiftC#JuliaGroovyRElixirHaskellKotlinScalaClassic ASPClipperMS-DOS
LESSONS
Introduction to JavaScriptVariables (let / const)Data TypesOperatorsStringsArraysObjectsConditions (if / else)Loops (for / while)FunctionsArrow Functions (=>)Array Methods (map / filter / reduce)
REFERENCE
📖 Overview
🖥️ Output & Console
📦 Variables
🔢 Numbers & Math
✍️ String
📋 Array
🗂️ Object
🔁 Condition & Loop
🧩 Function
🔄 Type & Conversion
JavaScript›Arrow Functions (=>)

Arrow Functions (=>)

short function definition without writing function; ideal for one-liners and use with array methods.

Simple enough for a 5-year-old to understand

Arrow function syntax

short functions are written with =>. If there is a single expression, { } and return are not needed.

Example code
1
const square = x => x * x;
"take x, return its square" — without writing function.
Function"take x, return its square" — without writing function.
2
console.log(square(6));
36
Output / Input36
FunctionOutput / Input
💡 Hint: Parentheses for multiple parameters: (a, b) => a + b.

🎯 Exercise & Practice

Test what you've learned — answer, write code, check instantly. (1 left)

1.Write an arrow function that returns 3 times a number (tripleIt); print the result of tripleIt(7).

🎯 Expected output: 21

Loading editor…

About this topic

The arrow function is a shorter function syntax added to the language in 2015. The arrow sign is used instead of the function keyword; in single-line functions, there is not even a need to write curly braces and return. When used with array methods, the code becomes significantly shorter.

However, the arrow function is not just a short syntax; its behavior is also different. In classic functions, the this keyword changes based on HOW the function is called, and this is one of the most confusing topics in JavaScript. In arrow functions, this comes from where it is defined and does not change — this directly eliminates many classic bugs.

This difference is both an advantage and a constraint. If you write an arrow function as a method of an object, this will not point to that object; a classic function is needed there.

What you will learn

  • The arrow function syntax and short return format
  • That parentheses are optional for a single parameter
  • The need to use parentheses when returning an object
  • How this behavior differs from a classic function
  • When to choose an arrow function versus a classic function

When to use?

Arrow functions are ideal for short transformations passed to array methods, event listeners, and timers. Especially if you are using a timer or event listener inside a class method, the arrow function keeps this correct.

If you are defining a method inside an object or if the function needs its own this, a classic function should be used. There is also a short method syntax for object methods, which is the most readable option.

Common mistakes

Wrong

const makeUser = (name) => { name: name };
console.log(makeUser("Ada"));

The curly brace after the arrow is considered the FUNCTION BODY, not an object. The function returns nothing and undefined comes back. To return an object, you need to enclose the curly braces in normal parentheses.

Correct

const makeUser = (name) => ({ name: name });
console.log(makeUser("Ada"));

Wrong

const counter = {
  count: 0,
  increase: () => {
    this.count++;
  },
};

In an arrow function, this comes from the outer scope where it is defined and does not point to the object; count does not increase. You should avoid using arrow functions in object methods.

Correct

const counter = {
  count: 0,
  increase() {
    this.count++;
  },
};

Wrong

const double = (n) => {
  n * 2;
};
console.log(double(4));

The moment you use curly braces, the automatic return disappears; since you did not write return, undefined comes back. Omitting curly braces entirely in single-line transformations is both short and safe.

Correct

const double = (n) => n * 2;
console.log(double(4));

Frequently asked questions

Are arrow functions always better?

No. They are more readable for short callbacks, but wrong in object methods and functions that need their own this. Also, arrow function names do not always appear clearly in error traces; using declarations in complex functions makes debugging easier.

Why is this so confusing?

Because in classic functions, this depends on WHERE the function is CALLED, not where it is defined. When you call the same function in different ways, this points to a different thing. Arrow functions eliminate this problem by fixing this binding to where it is defined.

Should I write parentheses for a single parameter?

From a language perspective, it is optional. However, writing them consistently is a common preference; when you add parameters or write type information, parentheses will already be required. Most formatting tools also add parentheses by default.

Summary

The arrow function provides short syntax and fixes this to where it is defined; this prevents classic bugs in event listeners and timers. In object methods, however, it is wrong — the short method syntax should be used there.

HexLibraryHexLibrary

Learn programming more clearly. Every command is explained line by line with colors, reinforced with runnable exercises, test your code instantly — all in one place, free.

Learn

  • Home
  • Languages & Lessons
  • HexCode

Company

  • About
  • Contact

Legal

  • Privacy Policy
  • Terms of Use

Other Projects

  • HexToolz
  • Free Service Manuals
© 2026 HexLibrary · The clearest way to learn code