Common Errors Caught by JavaScript Strict Mode
See the most common mistakes that JavaScript strict mode catches, including undeclared variables, read-only assignments, duplicate parameters, and more.
JavaScript strict mode errors are mistakes that strict mode turns into clear failures. Without strict mode, some of these mistakes are ignored or behave in confusing ways.
This article shows the common errors beginners are most likely to meet.
Quick Reference
| Mistake | Strict Mode Result |
|---|---|
| Assigning to an undeclared variable | ReferenceError |
| Writing to a read-only property | TypeError |
| Deleting a plain variable | SyntaxError |
| Duplicate parameter names | SyntaxError |
| Legacy octal literal | SyntaxError |
| with statement | SyntaxError |
| Plain function this | undefined |
Use this table as a map. The sections below explain the errors that matter most in beginner code.
Mistake 1: Undeclared Variables
Forgetting a declaration keyword is one of the most common strict-mode catches.
"use strict";
function setScore() {
score = 100;
}
setScore();This throws a ReferenceError because score was never declared. Declare the variable before assigning to it.
Mistake 2: Writing to Read-Only Properties
Strict mode throws when you try to write to a property that cannot be changed.
"use strict";
const config = {};
Object.defineProperty(config, "apiKey", {
value: "abc123",
writable: false,
});
config.apiKey = "xyz789";The final line throws a TypeError. The property exists, but it was defined as not writable.
This is better than silent failure because you find the mistake immediately.
Mistake 3: Deleting Variables
Strict mode does not allow deleting a plain variable name.
"use strict";
let count = 10;
delete count;This is a SyntaxError. Use delete for object properties, not local variables.
For example, deleting an object property can be valid if the property is configurable. Deleting a local variable is not.
Mistake 4: Duplicate Parameter Names
Strict mode rejects functions that repeat a parameter name.
"use strict";
function add(value, value) {
return value + value;
}This is a SyntaxError before the function runs. The fix is to give each parameter a unique name.
Duplicate names hide values and make function calls hard to reason about.
Mistake 5: Legacy Octal Literals
Old JavaScript allowed number literals with a leading zero in some non-strict scripts. Strict mode rejects that legacy form.
"use strict";
console.log(0123);This is a SyntaxError. If you intentionally want octal, use the modern zero-o prefix instead.
Mistake 6: with Statements
The with statement changes how variable lookup works, which makes code hard to understand and optimize. Strict mode forbids it.
"use strict";
const user = { name: "Alice" };
with (user) {
console.log(name);
}This is a SyntaxError. Write the property access directly instead, such as user.name.
Mistake 7: Plain Function this
Strict mode changes plain function calls so this stays undefined instead of falling back to the global object.
"use strict";
function showThis() {
console.log(this);
}
showThis();The output is undefined. This helps prevent accidental writes to the global object.
What Strict Mode Does Not Catch
Strict mode is useful, but it is not a full bug detector. It does not know that your formula is wrong, your API URL is misspelled, or your UI state has the wrong value.
Use strict mode with a good debugging process and a linter. Strict mode catches language mistakes; your tests and tools catch the rest.
Once your code runs in strict mode, follow consistent variable naming conventions and a practical JavaScript code style guide.
Rune AI
Key Insights
- JavaScript strict mode errors are often mistakes that sloppy mode ignores.
- Assigning to an undeclared variable throws a ReferenceError.
- Writing to read-only or getter-only properties throws a TypeError.
- Deleting plain variables is a SyntaxError in strict mode.
- Duplicate parameter names and with statements are rejected.
Frequently Asked Questions
Will strict mode catch all JavaScript bugs?
Can strict mode break old code?
Should I use a linter with strict mode?
Conclusion
Strict mode catches mistakes that normal scripts may ignore, especially accidental globals, invalid deletes, duplicate parameters, unsafe property writes, and old syntax such as with statements.
More in this topic
JavaScript While Loop Explained: A Complete Guide
A while loop repeats code as long as a condition stays true. Learn the syntax, see practical examples, and understand when a while loop is the right choice over a for loop.
JavaScript Loops Tutorial: for, while, do while
Loops let JavaScript repeat code without writing it twice. Learn the three core loop types, what each one does, and when to choose one over another.
How to Loop Through Arrays Using JS for Loops Guide
Looping through arrays is the most practical use of JavaScript for loops. Learn how to access each element by index, avoid off-by-one errors, and choose between for, for...of, and forEach.