Strict Mode Is Always Enabled in Classes
You'll learn that JavaScript classes automatically run in strict mode, making your code safer without extra configuration.
The Automatic Safety Net
When you write code inside a class—whether in the constructor, methods, or anywhere within the class body—JavaScript automatically enforces strict mode. You don't need to add "use strict"; at the top; it's baked in.
This is different from regular functions or scripts, where strict mode is optional. Classes enforce it by design to prevent common mistakes and ensure cleaner code.
What Strict Mode Changes Inside Classes
Two key behaviors change:
1. this becomes undefined in detached methods
If you extract a method from a class instance and call it alone, this will be undefined instead of defaulting to the global object. This prevents accidental global pollution.
2. Variable declarations are stricter
You cannot accidentally create global variables by forgetting let, const, or var. The code will throw an error instead.
Why This Matters
Imagine classes as a "professional workspace" where certain safety rules always apply, while regular functions are like a casual space where you can opt-in to those rules. Classes chose strict mode as the default because they're designed for modern, maintainable code patterns.
You've already been writing strict mode code in all your classes without realizing it! This automatic enforcement catches bugs early (like accessing undefined instead of silently using the global object) and aligns with best practices.
Key Takeaway: Class bodies always run in strict mode automatically—no "use strict"; needed—making this safer and preventing accidental global variables.