What does use strict do in JavaScript
Learn what use strict does in JavaScript, how to enable it, and how strict mode changes error handling, scope, and best practices for safer, clearer code with practical examples.

use strict is a directive that enables strict mode in JavaScript, a stricter variant of the language that enforces safer coding by throwing errors on unsafe actions.
Why Strict Mode Exists and What It Solves
According to JavaScripting, the JavaScripting team found that enabling strict mode helps catch common mistakes early and enforces safer coding practices. Strict mode, introduced in ES5, changes certain JavaScript semantics to prevent silent failures and to make errors explicit. By requiring declarations for all variables, prohibiting accidental global assignments, and disallowing some unsafe syntax, strict mode helps you write more robust code and simplifies debugging. In sloppy mode, some mistakes slip by and only reveal themselves much later, which can complicate maintenance. Strict mode also nudges developers toward clearer intent, making refactoring and collaboration easier. The tradeoffs include stricter syntax in some cases and potential breakage of legacy patterns, but the payoff is cleaner, more reliable code in both small scripts and large applications.
How to Enable Use Strict in JavaScript
There are a few common patterns for enabling strict mode. To opt in globally for a file, place 'use strict'; at the very top of the file before any other statements. To enable strict mode for a narrower scope, wrap the code in a function and insert 'use strict'; inside that function, for example:
'use strict';
function run(){
// strict mode applies here
var x = 1; // ok
}In modern JavaScript with modules, strict mode is automatic. Modules are always executed in strict mode by design, so you often do not need to add the directive manually. Build tools and linters can help enforce strict mode across a codebase, and many teams configure their editors to warn about implicit globals or risky patterns. When mixing strict and non strict code, pay attention to scope boundaries and testing to ensure consistent error reporting across the project.
Immediate Behavioral Changes You Will Notice
Strict mode changes several core behaviors in JavaScript. Some actions that silently succeed in sloppy mode now throw errors, helping you identify bugs earlier. For example, assigning to an undeclared variable creates a ReferenceError instead of creating a global variable. Deleting a non configurable property throws a TypeError. Duplicate parameter names in a function definition are rejected as a SyntaxError. In strict mode, this inside a function called without a binding to an object is undefined rather than the global object, which changes how methods and callbacks behave. The eval and arguments objects also behave differently: eval cannot introduce new variables into the local scope, and arguments are no longer aliased to parameters in a straightforward way. Finally, the with statement is disallowed, and octal numeric literals are rejected. These changes aim to surface mistakes that would otherwise be silent and hard to track.
Examples: Sloppy Mode vs Strict Mode
// Sloppy mode example
x = 10; // creates a global variable
function f(){ return x; }
f(); // 10
// Strict mode example
'use strict';
function g(){ return x; } // ReferenceError: x is not defined// Duplicate parameter names
function h(a, a){ return a; }
// In strict mode this is a SyntaxError// Deleting non configurable properties
"use strict";
delete Object.prototype; // TypeError in strict mode// The value of this in a bare function
function j(){ return this; }
console.log(j()); // undefined in strict mode, window in sloppy mode// eval and arguments
"use strict";
var x = 1;
eval("var x = 2;");
console.log(x); // 1, eval does not leak into the local scope// with statement forbidden
with (Math) { sin(π) } // SyntaxError in strict mode// Octal literals
var o = 077; // SyntaxError in strict modeQuestions & Answers
What is strict mode in JavaScript?
Strict mode is a way to opt into a restricted variant of JavaScript. It makes errors more visible by throwing exceptions for unsafe actions and disallowing certain syntax, leading to safer, more predictable code.
Strict mode makes JavaScript safer by throwing errors for risky actions and disallowing certain syntax.
How do you enable use strict in a file?
To enable strict mode globally, put 'use strict'; at the very top of the file. For scoped strict mode, place it inside a function to limit its effect to that function.
Put use strict at the top of the file or inside a function to limit its scope.
Do modules run in strict mode automatically?
Yes. JavaScript modules are always executed in strict mode by design, so you usually do not need to add the directive manually in modules.
Modules are strict by default, so you generally don’t need to add use strict there.
What are common errors when migrating to strict mode?
Common issues include undeclared variables, attempting to delete non configurable properties, and duplicate parameter names. ESLint and similar tools can help catch these patterns before runtime.
Typical migration issues include undeclared variables and duplicate parameter names that stricter coding will flag.
Does strict mode affect this binding in functions?
Yes. In strict mode, this inside a bare function equals undefined instead of the global object, which changes how methods and callbacks behave.
Strict mode makes this undefined in plain functions, changing how you access the global object.
Is use strict necessary in modern JavaScript?
In modern code, modules are already strict by default, so the global use strict directive is less critical for module code, but it is still valuable when working with legacy scripts or libraries.
For modules, strict mode is automatic; for legacy code, use strict is still a good practice.
What to Remember
- Enable strict mode at the top of files to catch errors early
- Expect undeclared variables to throw and duplicates to be rejected
- Modules are strict by default so use strictMore for legacy scripts
- Use tooling to enforce strict mode across a project