Skip to the workspace
Pebellslearn by building
Functions  ›  Lesson

Arrow Functions

Arrow Functions 12 min

javascript / Arrow Functions

Explore each scene at your own pace.

01 · Arrow Functions

Arrow Functions

Arrow Functions

An arrow function is a function expression with concise syntax and lexical this. An expression body returns its value; a block body needs an explicit return.

Changing the function’s braces can change whether a result is returned.

See the mechanism

Walk through the modelArrow FunctionsAuthored snapshots of the example; use Run below to test the code.
value3
result6
The expression is the return value.
1 of 3

Follow one example

javascript

double has an expression body and implicitly returns 6. broken has a block body: its calculation runs but no return supplies it to the caller, so the result is undefined. To return an object literal concisely, wrap it in parentheses. Arrow functions also capture this from their surrounding scope; do not replace every method with an arrow.

Predict, change and check

Predict: What single keyword would make the block-bodied version return 6?

Test your explanation: Add return inside broken. Then replace double with a block-bodied version and preserve the same result.

The tasks below ask you to explain that result and apply the idea to different inputs.

Apply and distinguish

Choose an arrow for the right binding behavior

An arrow function has lexical this and arguments rather than creating ordinary call-specific bindings for them. It is not a constructor and cannot be invoked with new. That makes it useful for callbacks but not a drop-in replacement for every method definition.

An expression body returns its expression; a block body needs an explicit return. Returning an object literal from an expression body needs parentheses so the braces are not parsed as a statement block. Inspect a callback that unexpectedly returns undefined before blaming the array method that calls it.

Check your understanding: State the input contract, identify the operation that changes the result, and give one boundary input that would distinguish the correct rule from the mistake described above.

Scene 1 of 2
Project context and further resources
Additional resources
Hover to wake the cannons. Drag to move, or use arrow keys when focused. Press Enter to fire a shot.