What does `"use strict"` do in JavaScript, and what is the reasoning behind it?
Naveera A.
—JavaScript developers often add the string "use strict"
in their code. What does "use strict"
do in JavaScript? What is the reason behind using it? Are there any benefits of using it?
The "use strict"
directive enables JavaScript’s strict mode.
JavaScript’s strict mode was introduced in ECMAScript 5. It enforces stricter parsing and error handling on the code at runtime. It also helps you write cleaner code and catch errors and bugs that might otherwise go unnoticed.
You can enable strict mode in two different ways, globally and locally.
Enable strict mode globally by adding the string "use strict"
as the first statement in your file. All subsequent code in the script will execute in strict mode.
// First line inside a .js file 'use strict'; // rest of the script
Enable strict mode locally by adding the string "use strict"
as the first statement inside a function. Using strict mode locally inside the function will enforce stricter parsing only within the context of that function.
// Inside a function function strict_function() { 'use strict'; // rest of the function }
It is important to note that JavaScript modules are in strict mode by default.
Enabling strict mode in your code has many benefits.
Here are some examples:
Prevent accidental creation of global variables
Strict mode is useful in situations where you can accidentally create a global binding. Accidental global variables can create bugs in the code. For example:
'use strict'; x = 9; // ReferenceError: x is not defined
function strict_function() { 'use strict'; x = 'I am a strict function'; console.log(x); } strict_function(); // ReferenceError: x is not defined
A common scenario in which you can accidentally create global variables is within for loops. With strict mode enabled, JavaScript will throw an error if you forget to add the let
keyword before the loop counter variable (counter
in the following example):
function catchTheProblem() { 'use strict'; for (counter = 0; counter < 10; counter++) { console.log('Catch me if you can!'); } } catchTheProblem(); // ReferenceError: counter is not defined
If you don’t use strict mode here, JavaScript will create a global variable with the name counter
.
Catch typing mistakes in variable names
If you don’t use strict mode, a typing error can create a new variable, which can cause bugs down the line. For example:
'use strict'; let misspelledVariable; misspelleVariable = 9; // This line will throw en error due to // misspelling of "misspelledVariable"
Prevent accidental deleting
Deleting a variable, a function, or an argument will result in an error:
'use strict'; let aVariable = 9; delete aVariable; //This will cause an error function testFunction() {} delete testFunction; // This will cause an error function testWithArgs(arg) { delete arg; // This will cause an error }
Prevent duplicating parameter names in a function
Duplicating the parameter’s names will result in an error:
function test(arg1, arg1) {} // This will cause an error
Prevent writing to read-only properties
If you set an object’s writable value to false
, and then try to assign a new value to the object, strict mode will throw an error:
'use strict'; const obj = {}; Object.defineProperty(obj, 'x', { value: 0, writable: false }); obj.x = 9; // This will cause an error
You can see more examples and benefits of using strict mode on w3schools and MDN documentation.
Tasty treats for web developers brought to you by Sentry. Get tips and tricks from Wes Bos and Scott Tolinski.
SEE EPISODESConsidered “not bad” by 4 million developers and more than 100,000 organizations worldwide, Sentry provides code-level observability to many of the world’s best-known companies like Disney, Peloton, Cloudflare, Eventbrite, Slack, Supercell, and Rockstar Games. Each month we process billions of exceptions from the most popular products on the internet.
Here’s a quick look at how Sentry handles your personal information (PII).
×We collect PII about people browsing our website, users of the Sentry service, prospective customers, and people who otherwise interact with us.
What if my PII is included in data sent to Sentry by a Sentry customer (e.g., someone using Sentry to monitor their app)? In this case you have to contact the Sentry customer (e.g., the maker of the app). We do not control the data that is sent to us through the Sentry service for the purposes of application monitoring.
Am I included?We may disclose your PII to the following type of recipients:
You may have the following rights related to your PII:
If you have any questions or concerns about your privacy at Sentry, please email us at compliance@sentry.io.
If you are a California resident, see our Supplemental notice.