Chapter XXIII · Error Handling
Strict Mode
Strict mode (`'use strict'`) opts a script or function into a stricter parsing and error-handling variant of JavaScript.
01 How It Works
Think of a safety inspector enforcing strict compliance rules on a factory workshop floor.
Strict mode catches silent developer mistakes, throws errors for unsafe actions (like global accidental variable assignments), and disables insecure features.
example.js
"use strict";
x = 10; // ReferenceError: x is not defined
1. Add `'use strict';` at the top of a script or function body.
2. Engine enforces strict validation rules.
3. Throws exceptions for unsafe coding practices.
02 Practical Example
Here is how you might see this concept applied in real-world code:
practical.js
"use strict";
function secure() {
let eval = 17; // SyntaxError in strict mode
}
Key Takeaway
Always use strict mode to write cleaner, safer, and more robust JavaScript code.