[JS] What Happens When “use strict” Is Defined
Tags: javascript, node, webdev
Published: 2024-01-05 02:35:32

JavaScript's `"use strict"` directive allows you to opt into a restricted variant of the language, enabling a more robust and error-prone coding environment. When `"use strict"` is defined, several changes occur in the behavior of your code, enhancing its reliability. In ES Modules available from (ES6)ES2015, it is always in strict mode though. In this post, I will explore various scenarios illustrating the impact of strict mode.

## Declaration Before Assignment

In strict mode, attempting to assign a value to a variable without prior declaration results in a ReferenceError. For instance:


```javascript
"use strict";
exampleVar = 1; // ReferenceError: exampleVar is not defined
```

Here, the absence of a declared variable `exampleVar` leads to a runtime error.

## Restrictions on delete Operator

Strict mode imposes restrictions on the use of the `delete` operator with variables. An attempt to delete a variable declared with `let` raises a SyntaxError:

```javascript
"use strict";
let exampleLet = 1;
delete exampleLet; // SyntaxError: Delete of an unqualified identifier in strict mode.
```

## Duplicate Parameter Names

Strict mode disallows the use of duplicate parameter names within function declarations:

```javascript
"use strict";
function exampleFunc(param1, param1) {} // SyntaxError: Duplicate parameter name not allowed in this context
```

This ensures parameter uniqueness and avoids potential confusion.

## Octal Literals

Old-style octal literals are prohibited in strict mode. Instead, you should use the modern `0o` prefix:

```javascript
"use strict";
let oldOctal = 010; // SyntaxError: Octal literals are not allowed in strict mode.
let modernOctal = 0o10; // OK
```

## Read-Only Properties

In strict mode, attempts to modify read-only properties result in a TypeError. Consider the following example:

```javascript
"use strict";
const object = {};
Object.defineProperty(object, 'propertyX', {
  value: 0,
  writable: false,
});

object.propertyX = 1; // TypeError: Cannot assign to read only property 'propertyX' of object '#<Object>'
```

In this case, it sets `writable: false`, the result might be taken for granted. However, the TypeError does not occur without `"use strict"`.

## Reserved Keywords as Variable Names

Certain keywords such as `private`, `package`, and `protected` are reserved in strict mode and cannot be used as variable names:

```javascript
"use strict";
var private = 1; // SyntaxError: Unexpected strict mode reserved word
```

## Prohibition of 'with' Statement

The `with` statement is forbidden in strict mode due to its potential for creating ambiguous code. Any attempt to use it will result in a SyntaxError:

```javascript
"use strict";
let area;
const radius = 1;
with (Math) {
  area = PI * radius * radius;
} // SyntaxError: Strict mode code may not include a with statement
```

## Limited Visibility of `eval` Declarations

Declarations made within `eval` statements are not visible outside of the `eval` scope:

```javascript
"use strict";
eval("var exampleVar = 1;");
console.log(exampleVar); // ReferenceError: x is not defined
```

Here, `exampleVar` is only defined within the `eval` statement, causing an error when trying to access it externally.

That is about it. Happy coding!