Learn JavaScript
❯ variables.strict_mode

Strict Mode in JavaScript

$concept

Strict mode ("use strict"; first line, or automatic inside modules and classes) converts silent sloppiness into errors: assignments to undeclared variables throw instead of creating globals, duplicate parameter names fail, this stays undefined (not window) in plain function calls, and octal literals plus with statements are banned.

There is no reason to write new sloppy-mode code — modules and classes opt in automatically, and explicit "use strict" covers scripts. Strict failures at development time replace global-namespace pollution and mystery-this bugs at run time.

$how it works
1

No accidental globals

x = 5; without declaration throws ReferenceError in strict mode — sloppy mode would silently create window.x.

2

Honest this

Plain function calls get this === undefined instead of the global object — masking bugs become immediate crashes.

3

Banned relics

with statements, octal literals, duplicate params, and deleting variables all fail — the error-prone corners, fenced.

4

Automatic in modules/classes

ES modules and class bodies are always strict — explicit directives matter only for classic scripts.

$example
"use strict";
try {
oops = 1;
} catch (e) {
console.log("caught:", e instanceof ReferenceError);
}
Output
caught: true
1The directive switches the whole script to strict semantics from the first line.
2Assigning undeclared oops throws ReferenceError instead of leaking a global.
3Sloppy mode would have printed nothing and polluted window — strict caught it.
$use cases
Starting every classic script with the directive (modules get it free)
Catching undeclared assignments during development, not production
Getting undefined-this failures instead of global-object surprises
Letting engines optimize harder under strict guarantees

Recommended resourcesAffiliate links · may earn us a commission

Recommended for learners

Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Pro code editor & AI assistantWrite, debug and explain code faster while you practice the patterns here.Try it free Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books

Practice what you learned

Re-implement each visualized algorithm in JavaScript yourself — building beats watching.