JavaScript Internals (521 Qs)•Part 2: Promises, Callbacks, Async & Strict Mode (54 - 103)
Why do you need strict mode
Strict mode is useful to write "secure" JavaScript by notifying "bad syntax" into real errors. For example, it eliminates accidentally creating a global variable by throwing an error and also throws an error for assignment to a non-writable property, a getter-only property, a non-existing property, a non-existing variable, or a non-existing object.
Practical Code Example
javascript
// Why Strict Mode is essential:
// In non-strict mode:
function loose() {
undeclared = 'leaked to global window!';
}
loose();
console.log(window.undeclared); // 'leaked to global window!' (silent bug)
// In strict mode:
function strict() {
'use strict';
// undeclared = 'error'; // ReferenceError: undeclared is not defined
}