Sentry Answers>JavaScript>

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

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.

Click to Copy
// 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.

Click to Copy
// 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:

Click to Copy
'use strict'; x = 9; // ReferenceError: x is not defined
Click to Copy
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):

Click to Copy
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:

Click to Copy
'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:

Click to Copy
'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:

Click to Copy
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:

Click to Copy
'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.

  • ResourcesImprove Web Browser Performance - Find the JavaScript code causing slowdowns
  • SentryJavascript Error Monitoring & Tracing
  • ResourcesJavaScript Frontend Error Monitoring 101
  • Syntax.fmListen to the Syntax Podcast
  • Syntax.fm logo
    Listen to the Syntax Podcast

    Tasty treats for web developers brought to you by Sentry. Get tips and tricks from Wes Bos and Scott Tolinski.

    SEE EPISODES

Considered “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.

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