What is JavaScript Use Strict and How to Use It
Discover what use strict does in JavaScript, how to enable strict mode, and practical tips to write safer, more robust code for modern apps today.

use strict is a directive that enables strict mode, a restricted variant of JavaScript that catches common errors and enforces safer coding practices.
What is strict mode and why it exists
If you ask what is javascript use strict, the short answer is that strict mode is a way to opt in to a restricted variant of JavaScript that eliminates some silent errors and changes how certain features behave. Developed as part of the ECMAScript 5 specification, strict mode helps developers catch mistakes early, write more predictable code, and avoid problematic language quirks. In practice, enabling strict mode is a decision about code quality; it does not change the core language semantics in every case, but it does tighten rules in ways that make bugs easier to spot. The JavaScripting team notes that adopting strict mode in new projects is generally a good practice for beginners and professionals alike because it enforces consistent coding patterns and improves maintainability. By understanding what strict mode changes, developers can write safer wrappers, modules, and libraries that behave consistently across environments. In addition, strict mode is helpful during debugging because it surfaces mistakes that would otherwise go unnoticed.
How to enable use strict
You enable strict mode in two primary ways. Global scripts and per function use cases have different implications for maintenance and readability.
- Global script: Place 'use strict'; at the very top of a file. This activates strict mode for the entire script and ensures uniform behavior across all functions in that file.
- Function scope: You can apply 'use strict'; inside a function to turn on strict mode only for that function. This lets you adopt strict mode gradually and test new patterns without rewriting the whole file.
In modern JavaScript, ES modules are always in strict mode by default, so additional 'use strict' declarations are often unnecessary in module code. Transpilers like Babel or TypeScript typically emit strict mode in their outputs, making explicit declarations redundant in many setups. A practical rule of thumb is to use modules whenever possible and liberal use of strict mode for legacy scripts that remain in classic script form.
What changes under strict mode
Strict mode tightens several rules and surfaces errors that otherwise go unnoticed. Here are the core changes you should expect:
- Declaring variables without var, let, or const becomes an error, preventing accidental creation of global variables.
- Assigning to undeclared variables throws a ReferenceError, helping you catch typos and missing declarations early.
- Deleting a non-configurable property throws a TypeError, making dangerous code paths easier to spot.
- Duplicate parameter names in functions are not allowed and result in a SyntaxError, encouraging clearer function interfaces.
- In non-method functions, this becomes undefined rather than the global object, which prevents accidental leaks.
- Octal literals are not allowed and attempting to use them triggers a syntax error.
- Writing to read-only properties or using certain eval-arguments patterns triggers errors.
These changes may seem strict, but they align JavaScript with safer practices and predictable behavior across engines.
Scope and modules compatibility
Scope in strict mode follows a predictable model. A 'use strict' declaration at the top of a function or script affects that scope only. In contrast, ES modules and modern build tools promote a module boundary where strict mode is implicit, allowing developers to rely on consistent semantics without repeated directives. This compatibility matters when migrating legacy code or integrating with third party libraries. For libraries intended for broad reuse, favor module syntax and explicit strict mode to minimize edge cases.
Common pitfalls and examples
To illustrate how strict mode works in practice, consider these common scenarios. They help explain why developers enable strict mode from the start:
// Example 1: Undeclared variable
x = 10; // ReferenceError in strict mode
// Example 2: Duplicate parameter names
function sum(a, a) { return a + a; } // SyntaxError in strict mode
// Example 3: Deleting non configurable property
delete Object.prototype; // TypeError in strict mode
// Example 4: this in a function
function f() { return this; }
console.log(f()); // undefined in strict mode when not called as a methodPerformance, tooling, and best practices
Strict mode has a small runtime overhead in some patterns but generally improves error visibility and code quality, which pays off in larger codebases. Use linting tools and rules to enforce strict mode consistently. ESLint, for example, can flag undeclared variables and duplicate parameters. Prefer module syntax wherever possible and reserve per function strict mode for legacy blocks you cannot convert yet.
Real world adoption and decision guidance
For new projects, enable strict mode on a per file or module basis and rely on modules to provide automatic strict mode. When maintaining older codebases, introduce 'use strict' gradually and verify behavior with tests and linters. The best practice is to adopt strict mode as part of your standard JavaScript toolkit, ensuring consistent behavior across browsers and environments.
Questions & Answers
What is use strict in JavaScript?
use strict is a directive that enables strict mode, a subset of JavaScript that enforces safer coding rules and helps catch errors early.
use strict enables strict mode, which enforces safer rules and helps catch errors early.
How do you enable strict mode?
You can enable it globally by placing 'use strict' at the top of a script or enable it for a function by placing the directive inside the function body. In ES modules, strict mode is automatic.
You enable strict mode with 'use strict' at the top of a file or inside a function; modules do this automatically.
What changes in strict mode?
Strict mode restricts certain actions like undeclared variables, duplicate parameter names, and deleting non configurable properties, making errors more visible.
Strict mode restricts undeclared variables and other risky actions, making errors more visible.
Is strict mode required in modules?
No, ES modules run in strict mode by default, so you do not need a separate directive in module code.
Yes, modules automatically run in strict mode, so the directive is often unnecessary there.
Does strict mode affect performance?
Strict mode can affect some patterns slightly, but it generally leads to safer code and easier maintenance, which benefits performance in the long run.
Strict mode mostly affects safety and maintainability; any performance impact is typically negligible.
Can I retroactively enable strict mode in old code?
Yes, you can gradually add 'use strict' directives file by file and rely on tests and linters to catch issues during migration.
You can migrate by adding 'use strict' in segments and testing as you go.
What to Remember
- Enable strict mode at the top of files or per function to tighten rules.
- Always declare variables with let or const in strict mode.
- Avoid unsafe patterns like deleting non configurable properties.
- Prefer modules to ensure implicit strict mode and future compatibility.
- Use linting and tests to catch strict mode errors early.