---
title: "What is JavaScript strict mode, and why is it used?"  
description: "What is JavaScript strict mode, and why is it used?"  
author: "Utpal Vishwas"  
published: 2023-10-08  
updated: 2023-10-09  
canonical: https://www.mindstick.com/forum/160061/what-is-javascript-strict-mode-and-why-is-it-used  
category: "javascript"  
tags: ["javascript", "strict mode"]  
reading_time: 3 minutes  

---

# What is JavaScript strict mode, and why is it used?

What is [JavaScript](https://www.mindstick.com/articles/874/how-to-create-watermark-text-for-textbox-by-using-javascript) [strict mode](https://www.mindstick.com/forum/160062/how-to-enable-strict-mode-in-javascript-and-what-are-its-effects-on-code-behavior), and why is it used?

## Replies

### Reply by Aryan Kumar

JavaScript [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) is a special mode in which you can run JavaScript code. It enforces stricter rules on your code, catches common coding mistakes, and prevents the use of potentially problematic features. It is used to improve code quality, maintainability, and to reduce the risk of subtle bugs in JavaScript programs. Here's an overview of JavaScript strict mode and why it is used:

## Enabling Strict Mode:

To enable strict mode, you simply add the following directive at the beginning of your JavaScript code (either at the top of a script or at the top of a function):

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

## Benefits of Strict Mode:

Strict mode provides several benefits:

- **Catching Common Mistakes:** It helps catch and report common programming mistakes and "unsafe" actions that might have gone unnoticed in non-strict mode. This includes typos in variable assignments, using undeclared variables, and assigning values to read-only properties.
- **Preventing Global Variables:** In strict mode, omitting the **var**, **let**, or **const** keyword when declaring a variable will result in a **ReferenceError**, preventing the accidental creation of global variables.
- **Disallowing Octal Literals:** Strict mode disallows octal literals (e.g., numbers with leading zeros like **0123**) to prevent unexpected behavior, as octal literals can behave differently in JavaScript.
- **Restricting this in Functions:** In non-strict mode, if a function is invoked without an object (e.g., as a standalone function), **this** refers to the global object (e.g., **window** in browsers). In strict mode, it is set to **undefined**, reducing the risk of accidentally modifying global variables.
- **Prohibiting Duplicate Parameter Names:** Strict mode prevents the definition of functions with duplicate parameter names, which can lead to confusion and bugs.
- **Making eval Safer:** In strict mode, the **eval** function is less error-prone. Variables and functions declared inside an **eval** are not created in the outer scope, limiting potential side effects.

## Why Is Strict Mode Used:

Strict mode is used for several reasons:

- **Code Quality:** It helps improve the overall quality of your code by catching errors and inconsistencies at an early stage of development.
- **Debugging:** Strict mode makes it easier to debug code by providing clearer error messages and preventing subtle bugs.
- **Maintainability:** It encourages writing code that is easier to read, understand, and maintain by enforcing good coding practices.
- **Compatibility:** It ensures code compatibility with future versions of JavaScript, as some behaviors that are allowed in non-strict mode are deprecated and may change in the future.
- **Security:** Strict mode can help reduce security vulnerabilities by preventing certain actions that could be exploited by malicious code.

In summary, JavaScript strict mode is a valuable tool for writing safer and more reliable JavaScript code. It enforces stricter rules, catches common mistakes, and encourages better coding practices, ultimately leading to more maintainable and robust applications. It's a recommended practice to enable strict mode in all your JavaScript code.


---

Original Source: https://www.mindstick.com/forum/160061/what-is-javascript-strict-mode-and-why-is-it-used

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
