You will be able to enable JavaScript strict mode in a script or function and explain how it catches common mistakes such as implicit global variables.
What it is
Strict mode is a way to opt into a stricter, safer variant of JavaScript. You enable it by placing the directive "use strict" at the beginning of a script or function body. It is not a function call; it is a string literal that JavaScript recognizes as a directive.
The mental model is: “run this code with fewer silent mistakes.” In normal, non-strict JavaScript, some errors are ignored or quietly converted into odd behavior. In strict mode, many of those cases throw errors instead.
Related terms include implicit global, directive prologue, function-level strict mode, and script-level strict mode. Modern ES modules and class bodies are already strict by default.
Why it matters
- It prevents accidental global variables, such as assigning to a misspelled variable name.
- It makes invalid code fail loudly instead of behaving mysteriously.
- It helps catch duplicate function parameters and some unsafe uses of
delete. - It changes the value of
thisin plain function calls toundefined, reducing accidental global access. - It encourages cleaner code by disabling some confusing legacy behaviors.
Syntax or steps
For an entire script, place the directive as the first statement:
"use strict";
For a single function, place it as the first statement inside the function body:
function example() {
"use strict";
// strict-mode code here
}
The directive must appear before executable code. Comments are allowed before it, but normal statements are not.
Example
"use strict";
function createGreeter(name) {
if (!name) {
name = "world";
}
return "Hello, " + name;
}
console.log(createGreeter()); // Hello, world
function leak() {
secret = 123; // ReferenceError in strict mode
}
try {
leak();
} catch (error) {
console.log(error.name); // ReferenceError
}
Part by part:
"use strict";enables strict mode for the whole script.createGreeter()works normally and returns a greeting.secret = 123;tries to assign to an undeclared variable.- In non-strict mode, that would create a global variable called
secret. - In strict mode, it throws a
ReferenceError. - The
tryblock catches the error so the example can print the error name.
Common mistakes
- Placing
"use strict"after code. Fix: put it as the first statement in the script or function. - Thinking function-level strict mode affects the whole file. Fix: use script-level strict mode or modules if you want the entire file strict.
- Assuming strict mode prevents all bugs. Fix: it catches certain mistakes, but you still need validation, tests, and careful design.
- Adding it to ES modules expecting a change. Fix: modules are already strict, so the directive is usually unnecessary.
When to use it
Use strict mode when writing plain JavaScript scripts where you want safer behavior. Compare it with non-strict mode and modules:
| Context | Recommended approach | Why |
|---|---|---|
| New plain script | Add "use strict" at the top |
Catches implicit globals and other mistakes early |
| ES module | No directive needed | Modules are strict by default |
| Legacy script | Add carefully and test | Strict mode may reveal existing bugs |
| Small function | Use function-level strict mode | Applies stricter rules only where needed |
Practice
Guided exercise: Write a function that accidentally assigns to an undeclared variable. Add "use strict" at the top of the script, call the function inside a try block, and print the error name.
Expected output: ReferenceError.
Challenge: Create a function-level strict function called sum() that returns the sum of two numbers and does not create any global variables.
Solution hint: place "use strict"; as the first line inside the function body.
Quick check
Question: Where must "use strict" appear to enable strict mode for a function?
Answer: It must be the first statement in the function body, before any executable code.
Summary
Strict mode is a simple opt-in that makes JavaScript reject several unsafe or confusing behaviors. The key idea is to fail early instead of allowing silent mistakes like accidental globals.