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.
Make this function pure: let count = 0; function next() { count++; return count; } (hint: take the current count as a parameter).
Write a pure applyDiscount(product, percent) that returns a new product object with a reduced price, leaving the original untouched.
Rewrite this IIFE with a block and const: (function () { var secret = 42; console.log(secret); })();
Explain it without notes
Define a pure function and give one example of a pure and one of an impure function.
List five kinds of side effects a JavaScript function can have.
Why can a function change an array passed to it, and how do you avoid it?
What is an IIFE, why was it widely used, and what replaces it today?
Preview
Console
Nothing logged yet.Expected console output
750 675
₹675.00