JavaScript `use strict` Explained in 2 Minutes

<!-- **This post is originally from [](, a blog about CSS, JavaScript, and just about anything programming.** -->

Go check out Daily Developer Jokes, my latest project!

Daily Developer Jokes Website Follow Daily Developer Jokes on DEV

Here's the joke from today:

Daily Developer Jokes, Friday Jan. 17, 2020

JavaScript is a very lenient language in terms of how it is interpreted. For example:

1x = 5; 2

Is not valid JavaScript code, and should be written as var x = 5 (or const/let in ES6), but the JavaScript interpreter still allows this and gives no errors.

Simply put, in general, normal JavaScript allows for code that is badly written and includes bad syntax.

use strict Solves This Problem

Introduced in ES5, the use strict directive provides a way to tell the interpreter to turn badly written JavaScript into errors.

This forces developers to write cleaner, more organized, and more readable code in the process. In fact, use strict is used by many famous JavaScript libaries such as ReactJS, jQuery, and more.

Written with the line "use strict";

The following line is used to enable use strict in the current function scope.

1"use strict"; 2 3// strict code here 4

Use of use strict in a specific function looks like this:

1function myFunc(){ 2 "use strict"; 3 4 // strict code here 5} 6

Usage in the global scope is generally not used, because strict code prevents global variables (elaborated on later).

Instead, it is common practice to use use strict within a JavaScript IIFE (immediately invoked function expression), like this:

1// non-strict code here 2 3(function(){ 4 "use strict"; 5 6 // strict code here 7})(); 8 9// non-strict code here 10

The "use strict"; line is a JavaScript literal expression, ignored by versions of JavaScript that don't support it.

use strict is supported by all major browsers (see CanIUse Data).

Consider the Following Example:

Non-Strict Code Example

Is non-strict code, and creates several potential problems:

  • Creates a variable without a proper var (or let/const in ES6) declaration
  • Creates a global variable which could lead to unclean or hard-to-maintain code
  • Uses the delete keyword to delete a variable, rather than letting JavaScript's garbage collector do that automatically.

Using use strict forces that code to be written more like this:

Strict Code Example

What Exactly use strict Prevents

Below is a brief list of the main features that strict mode includes:

  • Forces proper declaration of variables (e.g. x = 1;)
  • Prevents global variables
  • Blocks repeated object property names (e.g. var obj = {p1: 5, p1: 7, p2: 9};)
  • Blocks assignment to non-writable global variables (e.g. undefined = 1;)
  • Prevents use of octal numbers (e.g. var x = 0o144;)

This is not a full list, and you can read more about the exact functionality of use strict in the Use Strict MDN Web Docs Page.

I hope you enjoyed this article and found use strict to be something you may use in the future.

<!-- **This post is originally from [](, a blog about CSS, JavaScript, and just about anything programming.** -->

Thanks for scrolling.

— Gabriel Romualdo, January 17, 2020