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

strict mode · "use strict"

LambdaVSThe Mutator
Lambda says"use strict" opts into stricter parsing that catches silent mistakes.

Adding "use strict" at the top of a file or function turns silent errors into thrown ones: assigning to undeclared variables, duplicate parameters, and writing to read-only properties all fail loudly. Modules and class bodies are strict automatically, so you'll often get it for free in modern code.

Power-ups you unlock

The Mutator attacks — common mistakes

Boss battleIn your head: in strict mode, assigning to an undeclared variable throws — why is that good?

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>'use strict';
let msg; try{ undeclared = 5; }catch(e){ msg='strict caught: '+e.name; }
document.getElementById('o').textContent=msg;</script></body></html>
▶ Open the interactive comic issue
‹ NaN · Why IsNaN Is WeirdModules · Import · Export ›