---
title: "Explain the difference in variable declaration behavior between strict mode and non-strict mode."  
description: "Explain the difference in variable declaration behavior between strict mode and non-strict mode."  
author: "Sandra Emily"  
published: 2023-10-08  
updated: 2023-10-09  
canonical: https://www.mindstick.com/forum/160064/explain-the-difference-in-variable-declaration-behavior-between-strict-mode-and-non-strict-mode  
category: "javascript"  
tags: ["javascript", "strict mode"]  
reading_time: 3 minutes  

---

# Explain the difference in variable declaration behavior between strict mode and non-strict mode.

[Explain the difference](https://www.mindstick.com/forum/156125/can-you-explain-the-difference-between-organic-and-paid-results) in [variable](https://www.mindstick.com/articles/1807/objective-c-data-types-variables-object-creation) declaration [behavior](https://www.mindstick.com/forum/159354/runtimeexception-and-exception-behavior) between [strict mode](https://www.mindstick.com/forum/160061/what-is-javascript-strict-mode-and-why-is-it-used) and non-strict mode.

## Replies

### Reply by Aryan Kumar

The behavior of variable declaration in JavaScript differs between [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) and non-strict mode. Here are the key differences:

## Implicit Global Variables:

**Non-Strict Mode:** In non-strict mode, if you declare a variable without the **var**, **let**, or **const** keyword, it becomes an implicit global variable, meaning it's added to the global object (e.g., **window** in browsers). This can lead to unexpected behavior and hard-to-debug issues.

```plaintext
"use strict";
myVar = 10; // Error: myVar is not defined
```

**Strict Mode:** In strict mode, declaring a variable without **var**, **let**, or **const** results in a **ReferenceError**. This helps prevent accidental creation of global variables and encourages explicit variable declarations.

```plaintext
myVar = 10; // Creates an implicit global variable in non-strict mode
console.log(window.myVar); // 10 (in a browser environment)
```

## Octal Literal Syntax:

**Non-Strict Mode:** In non-strict mode, JavaScript allows the use of octal literals (numbers with leading zeros like **0123**).

```plaintext
"use strict";
const octalNumber = 0123; // Error: Octal literals are not allowed in strict mode
```

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

```plaintext
const octalNumber = 0123; // Valid in non-strict mode
console.log(octalNumber); // Outputs: 83
```

## Duplicate Parameter Names:

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

```plaintext
"use strict";
function duplicateParam(x, x) {
  // Error: Duplicate parameter name not allowed in this context
}
```

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

```plaintext
function duplicateParam(x, x) {
  console.log(x); // Outputs the second 'x' parameter
}
```

## this in Global Scope:

**Non-Strict Mode:** 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:** In strict mode, when **this** is referenced in the global scope, it is **undefined**. This behavior helps prevent accidental modification of the global object.

```plaintext
"use strict";
console.log(this); // 'this' is undefined in the global scope
```

In summary, strict mode introduces stricter rules for variable declaration and usage to help catch common coding mistakes and promote better coding practices. It makes JavaScript less forgiving of certain types of errors and encourages a more controlled and predictable coding style. It's generally recommended to use strict mode in your JavaScript code to improve code quality and maintainability.


---

Original Source: https://www.mindstick.com/forum/160064/explain-the-difference-in-variable-declaration-behavior-between-strict-mode-and-non-strict-mode

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
