JavaScript `use strict` Explained in 2 Minutes
Tags: javascript, webdev
Published: 2020-01-17 19:21:41

---
title: "JavaScript `use strict` Explained in 2 Minutes"
published: true
description: "In general, normal JavaScript allows for code that is badly written and includes bad syntax. But, 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."
tags: #javascript, #webdev
cover_image: "https://xtrp.io/api/content/static_files/javascript-use-strict/photo2.jpg?v=1"
---

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

```JavaScript
x = 5;
```

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](https://reactjs.org/), [jQuery](https://jquery.org/), and more.

## Written with the line `"use strict";`

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

```javascript
"use strict";

// strict code here
```

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

```javascript
function myFunc(){
	"use strict";

	// strict code here
}
```

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:

```javascript
// non-strict code here

(function(){
	"use strict";

	// strict code here
})();

// non-strict code here
```

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](https://caniuse.com/#search=use%20strict)).

## Consider the Following Example:

![Non-Strict Code Example](https://xtrp.io/api/content/static_files/javascript-use-strict/non_strict.png?v=1)

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](https://xtrp.io/api/content/static_files/javascript-use-strict/strict.png?v=1)

## 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](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Strict_mode).

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

Thanks for scrolling.

*&mdash; Gabriel Romualdo, January 17, 2020*