Command Palette

Search for a command to run...

Hectal
Back to the lesson: Topic 3.2 — Declarations, Expressions and Arrow Functions
JavaScript · Example 3 of 3 ES2015+

Where arrows behave differently

this gets a full topic in the objects phase. For now: arrows borrow it from where they are written.

JavaScript has three main ways to write a function: a declaration (function add() {}), a function expression stored in a variable, and a short arrow function ((a, b) => a + b); they differ in hoisting, this, arguments and whether they can be used with new.

Change the code and press Run (Ctrl+Enter). Predict what will happen first, then break it on purpose and read what changes. Your edits are saved and match the lesson page.

Ready for more? This phase has 6 hands-on exercises with automatic checks.

Practice questions

Write the code in the editor, run it, then open the model answer to compare.

01

Rewrite function isAdult(age) { return age >= 18; } as an arrow function with a concise body.

02

Write an arrow toPoint that takes x and y and returns an object { x, y } with an implicit return.

03

Use an arrow with map to turn [1, 2, 3] into ["#1", "#2", "#3"].

Explain it without notes

01

What is the difference between a function declaration and a function expression?

02

What is an implicit return, and how do you implicitly return an object from an arrow function?

03

Name the behavioural differences between arrow functions and regular functions.

04

When would you choose an arrow function, and when a regular one?

Where arrows behave differently ES2015+

Console

(nothing was printed)

Expected console output

regular got 3 arguments
Person { name: 'Asha' }
TypeError: arrow is not a constructor
arrow has prototype? false
arrow sees: Kitchen timer
regular sees: undefined