Command Palette

Search for a command to run...

Hectal
Back to the lesson: Topic 3.9 — Pure Functions and Side Effects
JavaScript · Example 3 of 4

A pure core with a thin impure shell

Only update touches the page. The two pure functions can be tested with plain calls and compared results.

A pure function always returns the same output for the same inputs and changes nothing outside itself; everything else a function does to the outside world is a side effect, and the IIFE is an older pattern that wrapped code in a function just to keep its variables private.

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

Make this function pure: let count = 0; function next() { count++; return count; } (hint: take the current count as a parameter).

02

Write a pure applyDiscount(product, percent) that returns a new product object with a reduced price, leaving the original untouched.

03

Rewrite this IIFE with a block and const: (function () { var secret = 42; console.log(secret); })();

Explain it without notes

01

Define a pure function and give one example of a pure and one of an impure function.

02

List five kinds of side effects a JavaScript function can have.

03

Why can a function change an array passed to it, and how do you avoid it?

04

What is an IIFE, why was it widely used, and what replaces it today?

A pure core with a thin impure shell

Preview

Press Run to see the page.

Console

Nothing logged yet.

Expected console output

750 675
₹675.00