JavaScript Strict Mode and Safer Semantics
Quick answer
Strict mode is a defined variant of JavaScript semantics that turns some silent mistakes into errors and restricts certain syntax. It is not a static type checker or a security sandbox.
How it works
Place the exact string literal "use strict"; in the initial directive prologue of a classic script or an ordinary function body with simple parameters. Modules and class bodies are strict automatically; those are covered later. For an ordinary function called without a receiver, strict mode preserves undefined as its this value, whereas non-strict code substitutes the global object. this is a special value supplied by a call, taught in depth later.
function strictRead() { "use strict"; return this;}console.log(strictRead());Expected output in Node.js:
undefinedThe call supplies no receiver.
"use strict";let count = 2;count = count + 3;console.log(count);Expected output in Node.js:
5Ordinary declarations and assignments still work.
Common mistakes
In strict code, assigning to an unresolvable name throws ReferenceError rather than implicitly creating a global property. Do not place the directive after ordinary executable statements and expect it to activate strict mode. It does not prohibit all unsafe operations.
Practice
Inspect a short script and identify where a directive must go. Declare every assigned name explicitly.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
JavaScript Strict Mode and Safer Semantics — Knowledge Check
Apply the taught concept and reason about the result.
Where should the strict-mode directive appear in an ordinary function with simple parameters?
What happens on assignment to an unresolvable name in strict code?
What does function f(){ "use strict"; return this; } console.log(f()); print?
Which code is strict automatically?
What does strict mode not provide?