JavaScript has evolved over time while keeping old code working. This meant that early mistakes in the language couldn’t be fixed without breaking existing programs.
That changed in 2009, with the release of ECMAScript 5 (ES5). It introduced "use strict" – a way to opt in to a safer, modern version of JavaScript.
Strict mode is enabled by placing "use strict" at the very top of your script or function:
"use strict";
// Your modern JavaScript code hereYou can also enable it inside a function:
function example() {
"use strict";
// strict mode only applies inside this function
}Strict mode won’t work if it’s not at the top:
alert("Some code");
("use strict"); // ❌ Ignored!✅ Only comments may appear before "use strict".
Once strict mode is enabled, you cannot disable it. There’s no "no strict" or similar directive.
Browser consoles don’t run in strict mode by default.
"use strict";
// your code hereUse Shift + Enter to input multiline code before running it.
If your console doesn’t behave well, wrap your code in an IIFE (Immediately Invoked Function Expression):
(function () {
"use strict";
// your code here
})();Yes – at least for now.
Strict mode helps catch bugs, improves performance in some cases, and prevents certain bad practices.
"use strict";
// safer and cleaner codeBut… when you start using classes or modules, strict mode is enabled automatically. So:
✅ Use "use strict" now.
💡 Later, with modern features, you won’t need it!
- Makes it easier to write secure JavaScript.
- Prevents the use of undeclared variables.
- Eliminates silent errors.
- Fixes mistakes in the language.
Stay strict, code safe! 💻🔒