freecoding.school100% FREE · NO SIGNUP
Closure CavesISSUE #4 of 61

return values · explicit vs implicit

LambdaVSThe Mutator
Lambda saysreturn sends a value back and stops the function; no return yields undefined.

return value hands a result to the caller and immediately exits. A function with no return (or a bare return) yields undefined. Watch automatic semicolon insertion: a return alone on its line returns undefined even if a value follows on the next line.

Power-ups you unlock

The Mutator attacks — common mistakes

Boss battleWrite a function that returns early for invalid input, otherwise computes a result.

Example code

<!doctype html><html><head><meta charset="utf-8"></head>
<body style="background:#06040d;color:#e6e0ff;font-family:monospace;padding:20px"><pre id="o"></pre>
<script>function half(n){ if(typeof n!=='number') return 'invalid'; return n/2; }
document.getElementById('o').textContent=half(10)+' · '+half('x');</script></body></html>
▶ Open the interactive comic issue
‹ Parameters · Default · RestScope · Global · Function · Block ›