Module 23: Functions and Scope

Lesson 2 of 5 · 15–25 minutes

Arrow functions in depth

🏠The everyday picture

A formal letter and a text message can say the same thing. Arrow functions are the short form, and they're everywhere in modern code.

📘What it really is

function square(n) { return n * n; }
const square = (n) => { return n * n; };
const square = n => n * n;

🖱️Try it

Rewrite addGst and addDelivery as one-line arrow functions. Write sumAll(...numbers) that adds any number of arguments, and makeLine(name, qty) that returns an object { name, qty } in one line. Test each.

✅Quick check

const f = () => { total: 0 }; returns undefined. Why?

Show the answer

The braces are treated as a code block, not an object. Write () => ({ total: 0 }).

Does n => { n * 2 } return anything?

Show the answer

No. With curly braces you need return.

← PreviousFunctions are values
ProLearners Assist