---
title: "How to use strict mode to catch errors in javascript code?"  
description: "How to use strict mode to catch errors in javascript code?"  
author: "Utpal Vishwas"  
published: 2023-10-08  
updated: 2023-10-09  
canonical: https://www.mindstick.com/forum/160063/how-to-use-strict-mode-to-catch-errors-in-javascript-code  
category: "javascript"  
tags: ["javascript", "strict mode"]  
reading_time: 2 minutes  

---

# How to use strict mode to catch errors in javascript code?

How to use [strict mode](https://www.mindstick.com/forum/160061/what-is-javascript-strict-mode-and-why-is-it-used) to [catch](https://www.mindstick.com/forum/159341/how-to-use-try-and-catch-in-java-for-exception-handling-and-what-happens-when-an-exception-occurs) [errors](https://answers.mindstick.com/qa/116170/fresh-fir-against-gandhis-in-national-herald-case-cover-up-for-ed-s-own-errors) in [javascript](https://www.mindstick.com/articles/874/how-to-create-watermark-text-for-textbox-by-using-javascript) [code](https://yourviews.mindstick.com/view/85458/alan-turing-the-mastermind-behind-cracking-the-enigma-code-during-world-war-ii)?

## Replies

### Reply by Aryan Kumar

You can 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 to catch errors and prevent certain common coding mistakes. Strict mode helps you write cleaner and more reliable code by making JavaScript less forgiving of certain types of errors. To enable strict mode, you need to include the following line of code at the beginning of your script or function:

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

Here's how to use strict mode to catch errors in your JavaScript code:

## Script-Wide Strict Mode:

- If you want to enable strict mode for an entire JavaScript file, place the **"use strict";** statement at the very beginning of the file before any other code.

```plaintext
"use strict";

// Your JavaScript code goes here
```

## Function-Wide Strict Mode:

- You can also enable strict mode for a specific function by placing the **"use strict";** statement at the beginning of the function.

```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";**.

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

Once you've enabled strict mode, it provides the following benefits:

- **Variable Declaration:** It enforces variable declaration with **var**, **let**, or **const**, preventing the accidental creation of global variables.
- **Octal Literal Syntax:** It disallows octal literals, preventing syntax errors caused by numbers with leading zeros.
- **Duplicate Parameter Names:** It prevents the definition of functions with duplicate parameter names.
- **this in Global Scope:** It sets **this** to **undefined** in the global scope, reducing the risk of accidental modification of the global object.
- **Assignment to Undeclared Variables:** It disallows assignment to undeclared variables, helping you catch typos and potential issues.
- **Read-Only Global Objects:** It makes some global objects (e.g., **NaN**, **Infinity**, **undefined**) read-only, reducing the risk of unintentional reassignment.

Enabling strict mode is a good practice in modern JavaScript development, as it helps you catch errors early in the development process and encourages writing cleaner, more maintainable code


---

Original Source: https://www.mindstick.com/forum/160063/how-to-use-strict-mode-to-catch-errors-in-javascript-code

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
