What is strict mode in JavaScript and why should you use it?

Srijan } Author Pic
SrijanFeb 28, 2025 - JavaScript
What is strict mode in JavaScript and why should you use it? - Reacted Node

Photo: Jadson Thomas/Pexels

Introduction

Like any evolving technology, JavaScript has undergone significant changes over time. Early versions of JavaScript, while groundbreaking for their time, had some quirks and inconsistencies. As language matured, designers recognized these flaws and sought to correct them. However, a crucial challenge arose: maintaining backward compatibility. Breaking existing code to fix these issues wasn't an option.

The solution? Strict Mode. Introduced in ECMAScript 5 (ES5), strict mode is a way to opt into a cleaner, more robust version of JavaScript. Many of the historical quirks are disabled in the strict mode, encouraging better coding practices and preventing standard errors.

Table of Contents

How to use strict mode

To activate the strict mode, add the use strict directive at the beginning of your JavaScript file or within a function.

1"use strict"; // Strict mode enabled for the entire file
2
3// Your strict mode code here...
4let myVariable = "Hello"; // This is okay
5myVariable = "World"; // This is also okay
6
7function myFunction() {
8 "use strict"; // Strict mode enabled only for this function
9
10 let functionVariable = 10;
11 functionVariable = 20;
12}
13
14x = 5; // This will cause an error in strict mode because x is not declared

Strict mode and modern JavaScript (ES6+)

With the introduction of ES6 (ECMAScript 2015) and later versions, many new language features implicitly invoke strict mode. For instance, when you use the class keyword to define a class or create an ES6 module, all the code within that class or module is automatically in strict mode. This means that older, less strict behaviors are unavailable in these modern contexts.

Why strict mode matters

Relying on some of JavaScript's older, less strict behaviors can lead to unexpected bugs and headaches down the line. Strict mode helps you build a more solid foundation for your code by:

Catching common coding mistakes

Strict mode converts silent errors into actual errors, providing immediate feedback and helping you identify potential problems early in development.

Accidental global variables

Accidental global variables occur when you assign a value to a variable that hasn't explicitly declared using var, let, or const. JavaScript will automatically create a global variable with that name in non-strict mode.

1// Non-strict (silent error - creates a global variable):
2function myFunction() {
3 x = 10; // No 'var', 'let', or 'const'
4}
5myFunction();
6console.log(x); // Output: 10 (global variable created)

In strict mode, if you try to assign a value to an undeclared variable, JavaScript will throw a ReferenceError, preventing the creation of the global variable and alerting you to the problem.

1// Strict (error):
2"use strict";
3function myFunction() {
4 x = 10; // No 'var', 'let', or 'const'
5}
6myFunction(); // Throws ReferenceError: x is not defined

Assignment to a Read-Only Property

In non-strict mode, it will silently fail if you try to assign a new value to a read-only property. There's no error; the assignment doesn't happen. This can be confusing because your code might appear to be working correctly, but the property remains unchanged.

1// Non-strict (silent fail - assignment does nothing):
2const obj = { a: 1 };
3
4// Make 'a' read-only
5Object.defineProperty(obj, "a", { writable: false });
6obj.a = 2;
7console.log(obj.a); // Output: 1 (assignment silently fails)

Strict mode addresses this issue by throwing a TypeError when you attempt to assign to a read-only property. This makes the error explicit, helping you identify and fix the problem quickly.

1// Strict (error):
2("use strict");
3const obj = { a: 1 };
4
5// Make 'a' read-only
6Object.defineProperty(obj, "a", { writable: false });
7obj.a = 2; // Throws TypeError: Cannot assign to read only property 'a' of object '#<Object>'

Preventing bad syntax

Strict mode disallows certain syntax that is considered poor practice or could lead to performance issues, encouraging you to write cleaner, more maintainable code.

Duplicate parameter names

Older versions of JavaScript (before strict mode) allowed you to have duplicate parameter names within a function definition. Having duplicate parameter names can lead to confusion and unexpected behavior. When you call the function with multiple arguments, it becomes unclear which argument corresponds to which parameter. This makes your code harder to understand, debug, and maintain, especially in complex functions.

1// Non-strict (allowed, but confusing):
2function myFunction(a, a, b) {
3 console.log(a, b);
4}
5myFunction(1, 2, 3); // Output: 2, 3 (last 'a' value wins)

Strict mode in JavaScript helps prevent this issue by throwing a SyntaxError if you try to define a function with duplicate parameter names. This enforces cleaner code and helps you avoid potential errors.

1// Strict (error):
2"use strict";
3
4// Throws SyntaxError: Duplicate parameter name a
5function myFunction(a, a, b) {
6 console.log(a, b);
7}

Octal Literals

In older versions of JavaScript (pre-strict mode), octal literals were allowed. They were denoted by a leading zero (0) followed by a sequence of octal digits (0-7). For example, 010 in octal represents the decimal number 8.

The use of octal literals can lead to confusion and errors, especially for developers who are not familiar with octal notation. It's easy to accidentally create an octal literal when you intended to write a decimal literal. For instance, if you write 012 intending it to be the decimal number 12, it will actually be interpreted as the decimal number 10 (because 12 in octal is 10 in decimal).

1// Non-strict (allowed, but easily mistaken):
2let num = 010; // Octal (base-8), so 010 is 8 in decimal
3console.log(num); // Output: 8

Strict mode in JavaScript disallows the use of octal literals. If you try to use an octal literal in strict mode, it will throw a SyntaxError, preventing potential errors and confusion.

1// Strict (error):
2"use strict";
3
4// Throws SyntaxError: Octal literals are not allowed in strict mode
5let num = 010;

Disabling fetures prone to errors

Strict mode in JavaScript helps improve the language by disabling certain features that are known to be problematic or prone to errors. These features might have been part of earlier JavaScript specifications but have since been identified as potentially confusing or harmful to code quality.

The with statement

The with statement was intended to provide a shorthand for accessing properties of an object. It created a new scope where the properties of the specified object were accessible without needing to repeatedly reference the object.

1// Non-strict (with statement allowed)
2const obj = { a: 1, b: 2 };
3with (obj) {
4 console.log(a + b); // Accesses obj.a and obj.b
5}

The with statement can make code harder to read and understand because it can be unclear where variables are coming from (the with block's scope or the surrounding scope). This ambiguity can lead to confusion and unexpected behavior.

Strict mode completely disallows the with statement, forcing you to write clearer code by explicitly referencing the objects you're working with.

1// Strict (with statement disallowed)
2"use strict";
3const obj = { a: 1, b: 2 };
4with (obj) {
5 // Throws a SyntaxError
6 console.log(a + b);
7}

Deleting a variable, function, or function argument

In non-strict mode, you can delete variables, functions, and function parameter without any errors. This behavior can lead to unexpected results and make your code harder to maintain.

1// Non-strict (deleting allowed)
2let myVar = 10;
3delete myVar; // No error
4console.log(myVar); // Output: 10

Strict mode helps prevent this issue by throwing a SyntaxError if you try to use the delete operator on a variable, function or function parameter. This enforces cleaner code and helps you avoid potential errors.

1// Strict (deleting disallowed)
2"use strict";
3let myVar = 10;
4delete myVar; // Throws a SyntaxError

Making code more secure

Strict mode in JavaScript also enhances security by disallowing certain actions that could potentially be exploited by malicious actors. By preventing these actions, strict mode helps protect your code and data from vulnerabilities.

eval() and arguments as variable names

In non-strict mode, you can use eval and arguments as variable names without any issues. This can be problematic because eval is a function that executes JavaScript code represented as a string, and arguments is an array-like object that holds the arguments passed to a function.

1// Non-strict (eval and arguments as variable names allowed)
2let eval = 10;
3let arguments = [1, 2, 3];

Strict mode in JavaScript prevents you from using eval and arguments as variable names, reducing the risk of accidental misuse and potential security vulnerabilities.

1// Strict (eval and arguments as variable names disallowed)
2"use strict";
3let eval = 10; // Throws a SyntaxError
4let arguments = [1, 2, 3]; // Throws a SyntaxError

Restricting eval()

The eval() function in JavaScript is a powerful but potentially dangerous feature. It allows you to execute arbitrary code represented as a string. While this can be useful in certain situations, it can also pose security risks and make your code harder to maintain and debug.

In non-strict mode, when you use eval(), it can modify the scope of the surrounding code. This means that variables and functions declared inside the eval() code can affect variables and functions outside of it, potentially leading to unexpected behavior and vulnerabilities.

1// Non-strict (eval allowed)
2let x = 10;
3eval("x = 20");
4console.log(x); // Output: 20

Strict mode in JavaScript restricts the use of eval() by creating a new scope for the code executed by eval(). This means that variables and functions declared inside the eval() code are not accessible outside of it, reducing the risk of unintended side effects and security vulnerabilities.

1// Strict (eval restricted)
2"use strict";
3let x = 10;
4eval("let x = 20");
5console.log(x); // Output: 10

this in Functions (preventing accidental global this)

In non-strict mode, if a function is called without an explicit this context, the this value defaults to the global object (window in browsers, global in Node.js). This can lead to accidental global this binding, where the function unintentionally modifies global variables or properties.

1// Non-strict (this might refer to the global object):
2function myFunction() {
3 console.log(this);
4}
5
6// In a browser, 'this' is the window object.
7myFunction();

Strict mode in JavaScript prevents this issue by setting the default this value to undefined when a function is called without an explicit this context. This helps avoid accidental global this binding and makes your code more predictable and secure.

1// Strict (this defaults to undefined):
2"use strict";
3function myFunction() {
4 console.log(this);
5}
6
7myFunction(); // Output: undefined