---
title: "How to enable strict mode in JavaScript, and what are its effects on code behavior?"  
description: "How to enable strict mode in JavaScript, and what are its effects on code behavior?"  
author: "Sandra Emily"  
published: 2023-10-08  
updated: 2023-10-09  
canonical: https://www.mindstick.com/forum/160062/how-to-enable-strict-mode-in-javascript-and-what-are-its-effects-on-code-behavior  
category: "javascript"  
tags: ["javascript", "strict mode"]  
reading_time: 3 minutes  

---

# How to enable strict mode in JavaScript, and what are its effects on code behavior?

How to enable [strict mode](https://www.mindstick.com/forum/160061/what-is-javascript-strict-mode-and-why-is-it-used) in [JavaScript](https://www.mindstick.com/articles/874/how-to-create-watermark-text-for-textbox-by-using-javascript), and what are its effects on [code](https://yourviews.mindstick.com/view/85458/alan-turing-the-mastermind-behind-cracking-the-enigma-code-during-world-war-ii) [behavior](https://www.mindstick.com/forum/159354/runtimeexception-and-exception-behavior)?

## Replies

### Reply by Aryan Kumar

To enable [strict](https://yourviews.mindstick.com/view/81367/usa-imposes-strict-ban-on-syria) [mode](https://yourviews.mindstick.com/view/81372/us-congress-should-go-in-remote-mode-for-proper-functioning) in JavaScript, you simply include the following line of code at the beginning of your script or function:

```plaintext
"use strict";
```

Here's how to enable strict mode and understand its effects on code behavior:

## Script-Wide Strict Mode:

To enable strict mode for an entire JavaScript file, place **"use strict";** at the very beginning of the file before any other code. For example:

```plaintext
"use strict";

// Your JavaScript code goes here
```

## Function-Wide Strict Mode:

You can enable strict mode for a specific function by placing **"use strict";** at the beginning of that function. This allows you to apply strict mode selectively to only certain parts of your code. For example:

```plaintext
function myFunction() {
  "use strict";

  // Strict mode is enabled for this function
}
```

## Module-Level Strict Mode (ES6):

In ECMAScript 6 (ES6) modules, strict mode is enabled by default for the entire module, so you don't need to explicitly add **"use strict";**. For example:

```plaintext
// ES6 module with strict mode enabled by default
export function myFunction() {
  // Strict mode is enabled for this module
}
```

Now, let's explore the effects of enabling strict mode on code behavior:

- **Variable Declaration:**

**Non-Strict Mode Behavior:** In non-strict mode, you can assign values to undeclared variables, which results in the creation of a global variable.

**Strict Mode Behavior:** In strict mode, assigning a value to an undeclared variable is not allowed, and it throws a **ReferenceError**.

- **Octal Literal Syntax:**

**Non-Strict Mode Behavior:** In non-strict mode, octal literals (e.g., numbers with leading zeros like **0123**) are allowed.

**Strict Mode Behavior:** In strict mode, octal literals are not allowed, and using them results in a syntax error.

- **Duplicate Parameter Names:**

**Non-Strict Mode Behavior:** In non-strict mode, it's possible to define functions with duplicate parameter names, with the last parameter name taking precedence.

**Strict Mode Behavior:** In strict mode, declaring a function with duplicate parameter names results in a syntax error.

- **this in Global Scope:**

**Non-Strict Mode Behavior:** In non-strict mode, when **this** is referenced in the global scope (outside of any function), it refers to the global object (e.g., **window** in browsers).

**Strict Mode Behavior:** In strict mode, when **this** is referenced in the global scope, it is **undefined**. This behavior helps prevent accidental modification of the global object.

- **Assignment to Read-Only Global Objects:**

**Non-Strict Mode Behavior:** In non-strict mode, you can reassign values to read-only global objects like **NaN**, **Infinity**, and **undefined**.

**Strict Mode Behavior:** In strict mode, these global objects become read-only, and attempting to reassign them results in a **TypeError**.

Enabling strict mode is a best practice in modern JavaScript development because it helps catch common coding mistakes, promotes better coding practices, and makes JavaScript more predictable and robust. It encourages writing cleaner and more maintainable code by reducing the risk of bugs and unintended behavior.


---

Original Source: https://www.mindstick.com/forum/160062/how-to-enable-strict-mode-in-javascript-and-what-are-its-effects-on-code-behavior

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
