JavaScript AcademyProfessional Engineering
Level 1 · FoundationsModule 5: Functions and Reusable LogicLesson 24 of 150About 40 minutes

Lesson 24: Arrow functions

01Opening question

Modern code is full of lines like prices.map(p => p * 1.2). What is that arrow, and when is it the right tool?

Keep the question in mind. By the end of the lesson you will be able to answer it, and prove it with working code.

02Start with working code

An arrow function with no braces returns its expression automatically.

const addTax = (amount) => amount * 1.2;
const greet = name => `Hi, ${name}`;
console.log(addTax(10));
console.log(greet("Ada"));

03Objective and prerequisites

Learning objective: Write arrow functions with block and concise bodies, return object literals correctly, pass arrow functions as callbacks, and know when a regular function is better.

You need: Lesson 21: Function declarations and expressions, Lesson 23: Return values

Skills practised: Functions, Arrow functions.

04Explanation

An arrow function is a shorter way to write a function expression: const square = (n) => n * n;.

With a concise body (no braces) the expression after => is returned automatically. With a block body (=> { ... }) you must write return yourself.

Brackets around a single parameter are optional (n => n * 2). With zero or several parameters they are required: () => 42, (a, b) => a + b.

Arrow functions shine as short callbacks, functions you pass to other functions: [1, 2, 3].map(n => n * 10).

05Definitions

Arrow function
A compact function expression written with =>.
Concise body
An arrow function body without braces; its expression is returned.
Block body
An arrow function body in braces; it needs an explicit return.
Callback
A function passed to another function to be called later.
Lexical this
Arrow functions use the this of the code around them.

Open the full glossary

06Visual learning object

Argument and parameter explorer. Compare a regular function and its arrow form with the same arguments.

Loading the interactive object…

Open the full Function argument explorer page with its guided experiment.

07Examples

ex-024-01Three ways to write one function Browser-executable JavaScript

Declaration, expression and arrow give the same result.

function areaA(w, h) {
  return w * h;
}
const areaB = function (w, h) {
  return w * h;
};
const areaC = (w, h) => w * h;
console.log(areaA(2, 3), areaB(2, 3), areaC(2, 3));
Sample input
None
Expected output
6 6 6
Common mistake
Forgetting brackets around two parameters: w, h => w * h.
Try this
Write a fourth version with a block body.
Source
examples/ex-024-01-three-forms.js (repository pending publication)

ex-024-02Block bodies need return Browser-executable JavaScript

Braces turn off the automatic return.

const withBraces = (n) => {
  n * 2;
};
const concise = (n) => n * 2;
console.log(withBraces(5), concise(5));
Sample input
None
Expected output
undefined 10
Common mistake
Adding braces to an arrow and forgetting return.
Try this
Fix withBraces by adding return.
Source
examples/ex-024-02-block-needs-return.js (repository pending publication)

ex-024-03Arrows as callbacks Browser-executable JavaScript

Short arrows are passed straight into array methods.

const prices = [10, 25, 4];
const withTax = prices.map((p) => p * 1.1);
const big = prices.filter((p) => p > 5);
console.log(withTax.map((p) => p.toFixed(2)).join(", "));
console.log(big.length);
Sample input
None
Expected output
11.00, 27.50, 4.40 2
Common mistake
Writing p > 5 in map, which gives true/false instead of filtering.
Try this
Use map to make labels like "$10".
Source
examples/ex-024-03-callbacks.js (repository pending publication)

ex-024-04Returning an object Browser-executable JavaScript

Wrap an object literal in parentheses after =>.

const makeItem = (name, qty) => ({ name, qty });
const item = makeItem("Pen", 3);
console.log(item.name, item.qty);
Sample input
None
Expected output
Pen 3
Common mistake
Writing => { name, qty }, which is a block and returns undefined.
Try this
Add a price property with a default of 1.
Source
examples/ex-024-04-return-object.js (repository pending publication)

08Guided exercises

Three exercises: explore and modify, complete the implementation, then build independently. Tests check behaviour, so any correct solution passes.

Find the missing return

Fix an arrow function with a block body.

const discounted = (price) => {
  price * 0.8;
};
console.log(discounted(50));

Convert to arrow functions

Rewrite function expressions as concise arrows.

const toCents = function (dollars) {
  return Math.round(dollars * 100);
};
const formatCents = function (cents) {
  return "$" + (cents / 100).toFixed(2);
};
console.log(formatCents(toCents(12.5)));

Product label maker

Combine arrow functions and callbacks.

const products = [
  { name: "Tea", price: 3.5 },
  { name: "Coffee", price: 4 },
  { name: "Juice", price: 2.75 }
];

09Prediction

Commit to an answer first, then run the real code to compare.

const a = () => 1;
const b = () => { 1; };
const c = () => ({ v: 1 });
console.log(a(), b(), c().v);

What is printed?

10Debugging investigation

New cart items are undefined

A helper that creates cart items returns undefined, so the cart shows Item: undefined.

const createItem = (name) => { name: name };
const item = createItem("Mug");
console.log(`Item: ${item?.name}`);

More cases in the Debugging Detective Laboratory

11Practical scenario

Formatting a price list

A menu screen turns raw prices into labels. Short arrow callbacks keep the transformation on one readable line.

const prices = [2, 3.5];
console.log(prices.map(p => `$${p.toFixed(2)}`).join(" "));

12Knowledge check

1. What is printed?

const sq = n => n * n;
console.log(sq(3));
  • 9
  • undefined
  • An error
  • 3

2. What is printed?

const sq = n => { n * n; };
console.log(sq(3));
  • 9
  • undefined

3. When are brackets around arrow parameters required?

  • Always
  • With zero or two or more parameters
  • Never
  • Only with strings

4. Why does make() return undefined?

const make = (id) => { id };
console.log(make(1));
  • Arrows cannot return objects
  • The braces are read as a block
  • id is not defined
  • It should use function

5. What is a callback?

  • A function that calls itself
  • A function passed to another function
  • A loop
  • A return value

6. An object method uses this.name. Which form fits?

  • An arrow function
  • A regular method
  • Either
  • Neither

13Solutions and tests

Each exercise's solution appears in the exercise once you have run its tests. Solutions are one possible answer: the tests accept any implementation with the same behaviour.

Automated acceptance tests for this lesson

These are the same checks the exercises run. They are visible on purpose: client-side tests help you learn and are not secret.

  • Find the missing return: Prints 40; Returns a value
  • Convert to arrow functions: Prints $12.50; No function keyword
  • Product label maker: Prints the labels; toLabel works alone; Uses map
  • ex-024-01: output matches the documented expected output
  • ex-024-02: output matches the documented expected output
  • ex-024-03: output matches the documented expected output
  • ex-024-04: output matches the documented expected output

Download the whole lesson folder (examples, exercises, solutions, tests) as ZIP

14Source files

The lesson lives in the companion repository at curriculum/level-01-foundations/module-05-functions-and-reusable-logic/lesson-024-arrow-functions/. Every example on this page is generated from those files.

The GitHub repository (https://github.com/lsvekis/JavaScript-Professional-Academy) has not been published yet, so these links will work once it is. Until then, use the ZIP download above.

15Summary and next steps

  • Arrows are compact function expressions.
  • Concise bodies return automatically; block bodies need return.
  • Wrap returned object literals in parentheses.
  • Use arrows for short callbacks, regular functions for methods that need this.

16AI learning prompts

Copy a prompt into the AI assistant of your choice. Then verify what it says by running code here. No AI service is connected to this page.

Concept explanation Arrow functions
Context: I know function expressions.
Task: Explain arrow syntax, concise and block bodies with five examples.
Constraints: Beginner level.
Expected behaviour: I can convert a function to an arrow.
Testing: Give me four to convert.
Code prediction Arrow drill
Context: I know arrow functions.
Task: Give me six arrows, some returning undefined by mistake, and ask what each returns.
Constraints: Wait for answers.
Expected behaviour: Include an object return.
Testing: Explain any I miss.
Debugging Undefined from an arrow
Context: My arrow function returns undefined.
Task: Help me find why with questions.
Constraints: No direct answer.
Expected behaviour: I fix it myself.
Testing: List the two common causes.
Exercise generation Callbacks
Context: I finished the label maker.
Task: Create three exercises using arrow callbacks with map and filter.
Constraints: Fictional data, exact output.
Expected behaviour: Each needs a named arrow function.
Testing: Include expected outputs.
Code review Arrow style
Context: My code mixes arrows and declarations randomly.
Task: Suggest a consistent style and explain it.
Constraints: Keep behaviour identical.
Expected behaviour: Each choice has a reason.
Testing: Give me a short style guide.