What does `"use strict"` do in JavaScript, and what is the reasoning behind it?

Naveera A.

The Problem

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 Solution

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.

Usage

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.

Benefits of Using Strict Mode

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.

Get Started With Sentry

Get actionable, code-level insights to resolve JavaScript performance bottlenecks and errors.

  1. Create a free Sentry account

  2. Create a JavaScript project and note your DSN

  3. Grab the Sentry JavaScript SDK

<script src="https://browser.sentry-cdn.com/7.112.2/bundle.min.js"></script>
  1. Configure your DSN
Sentry.init({ dsn: 'https://<key>@sentry.io/<project>' });

Loved by over 4 million developers and more than 90,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.

Share on Twitter
Bookmark this page
Ask a questionJoin the discussion

Related Answers

A better experience for your users. An easier life for your developers.

    TwitterGitHubDribbbleLinkedinDiscord
© 2024 • Sentry is a registered Trademark
of Functional Software, Inc.