Enable strict mode to catch silent bugs — accidental globals, duplicate parameters, and undeletable properties. Learn why ES modules are always strict.
Why: strict mode catches silent errors and disables legacy features. ES modules are always in strict mode — no need to add "use strict" when using import/export.
// Without strict mode, assigning to an undeclared variable
// creates a global property silently — a common source of bugs.
// With strict mode:
(function() {
'use strict';
// Catches accidental globals
try {
undeclaredVar = 10; // ReferenceError
} catch (e) {
console.log('Caught:', e.message);
}
// this is undefined in strict regular functions
function whatIsThis() { return String(this); }
console.log('this in strict fn:', whatIsThis()); // 'undefined'
// Duplicate parameter names cause a SyntaxError
// function bad(a, a) {} // SyntaxError in strict mode
// delete on a non-configurable property throws
try {
const obj = {};
Object.defineProperty(obj, 'x', { value: 1, configurable: false });
delete obj.x; // TypeError in strict mode
} catch (e) {
console.log('delete failed:', e.message);
}
console.log('strict mode active');
})();