Pebellslearn by building
0%
RoadmapsMatrixCoursesSandboxSign in
The DOM and Strict Mode  ›  Lesson

Strict Mode

Strict Mode 12 min"use strict" turns several of JavaScript's silent mistakes — an undeclared variable, a write to a frozen or read-only property, duplicate parameter names — into thrown errors instead of quiet failures.

Strict Mode

In short: JavaScript's default rules (sloppy mode) let several real mistakes pass silently — an undeclared variable quietly becomes a global, writing to a frozen property just does nothing. The directive "use strict";, placed at the top of a script or the top of a function body, turns those silent failures into thrown errors, right where the mistake happened. It also forbids a handful of confusing patterns outright, like duplicate parameter names. Modern JavaScript modules and classes are strict automatically; plain scripts and functions need to opt in.

Two boxes with the same typo volume = 80 (no let/const/var): sloppy mode quietly creates a global "volume" with no error, the typo hides forever; "use strict" throws a ReferenceError right away, the bug surfaces immediately on the line that caused it

Catch a missing declaration

The single most common thing strict mode catches: assigning to a name that was never declared, usually a typo for a local variable:

javascript

This throws ReferenceError: volume is not defined, immediately. Remove the "use strict"; line, and the exact same code runs with no error at all — it silently creates a new global property named volume. That silent version is the dangerous one: the typo survives, and volume can now be read or overwritten from anywhere else in the program.

Catch a write to a frozen object

Object.freeze locks an object's properties. Sloppy mode lets you "write" to a frozen property and simply ignores it; strict mode tells you:

javascript

This throws TypeError: Cannot assign to read only property 'title'.... Without "use strict" inside the function, frozen.title = "Red Dawn"; would run, change nothing, and give no hint that anything went wrong — frozen.title would still quietly read "Blue Hour" afterwards.

The trap: assuming strict mode is the default

Plain scripts and ordinary functions are sloppy unless told otherwise. Forgetting the directive means every one of these checks is off, and bugs that would have thrown immediately instead vanish into the program's state, to resurface as confusing symptoms far from their real cause. ES modules and class bodies are strict automatically — but a plain <script> or a function outside one is not, until you add "use strict"; yourself.

Quick practice

quick drill

What does "use strict"; volume = 80; do?

quick drill

In sloppy mode (no "use strict"), what does volume = 80; do, with no prior declaration?

quick drill

What happens when strict-mode code assigns to a property of a frozen object?

quick drill

Where does "use strict"; need to appear to apply to a whole function?

quick drill

Which of these is strict by default, without writing "use strict"?

🎯 Your turn

A function has the line volume = 80; with no let/const/var in front — a typo for a local variable. Explain what happens when this function runs normally, and what happens once "use strict"; is added, and why the difference matters for catching bugs.

Write your explanation and submit it — there's no code to run here, just your reasoning.

Practice — level up

0/1 solved

Write your answer, then compare it with a rubric and a model answer.

ExplainSame typo, two outcomes

A function has the line volume = 80; with no let/const/var in front — a typo for a local variable. Explain what happens when this function runs normally, and what happens once "use strict"; is added, and why the difference matters for catching bugs.