The Wayback Machine - https://web.archive.org/web/20240727073923/https://www.geeksforgeeks.org/strict-vs-non-strict-mode-in-javascript/
Open In App

Strict vs Non-strict Mode in JavaScript

Last Updated : 10 May, 2023
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report

JavaScript is a widely used programming language that allows developers to create dynamic and interactive web applications. It has two modes of operation: strict mode and non-strict mode. These modes affect how JavaScript code is executed, and understanding the differences between them is crucial for writing secure and reliable code.

Strict mode is a subset of JavaScript that provides better error checking and enforces stricter rules for coding. It was introduced in ECMAScript 5 (ES5), which is the fifth edition of the ECMAScript language specification. When strict mode is enabled, the JavaScript engine checks for syntax errors and runtime errors that would otherwise go unnoticed in non-strict mode. This makes it easier for developers to catch errors early in the development process, resulting in fewer bugs and better code quality.

Non-strict mode, on the other hand, is the default mode of operation in JavaScript. In this mode, the JavaScript engine allows for more flexible coding and provides fewer restrictions. This means that some code that would cause errors in strict mode may execute without any issues in non-strict mode. While this may seem advantageous at first, it can lead to unexpected behavior and security vulnerabilities.

To enable strict mode in JavaScript, you simply add the string “use strict” at the beginning of a script or function. Here are a few examples of how the ‘strict’ mode changes the behavior of JavaScript code:

1. Prevents the use of undeclared variables: In non-strict mode, you can use a variable without declaring it first, which can lead to unexpected behavior if you mistype the variable name. In strict mode, however, using an undeclared variable will throw a reference error.

Javascript




// Non-strict mode
x = 10;
console.log(x); // Outputs 10
  
// Strict mode
"use strict";
y = 20; // Throws a reference error


Output

10

2. Disallows deleting certain properties: In non-strict mode, you can delete properties of an object even if they are non-configurable, which can lead to unexpected behavior. In strict mode, attempting to delete a non-configurable property will throw a type error.

Javascript




// Non-strict mode
var obj = {};
Object.defineProperty(obj, "x", {
    value: 10,
    configurable: false
});
delete obj.x;
console.log(obj.x); // Outputs undefined
  
// Strict mode
"use strict";
var obj2 = {};
Object.defineProperty(obj2, "y", {
    value: 20,
    configurable: false
});
delete obj2.y; // Throws a type error


Output

10

3. Prevents duplicate function parameters: In non-strict mode, you can declare multiple parameters with the same name, which can lead to confusion and errors. In strict mode, however, duplicate parameters will throw a syntax error.

Javascript




// Non-strict mode
function foo(x, x) {
    return x + x;
}
console.log(foo(1, 2)); // Outputs 4
  
// Strict mode
"use strict";
function bar(y, y) { // Throws a syntax error
    return y + y;
}


Output

4

These examples demonstrate just a few of the ways that the ‘strict’ mode can help to catch errors and improve the reliability of JavaScript code.

Let’s take a look at some of the key differences between strict mode and non-strict mode.

  • Variable declaration: In strict mode, variables must be declared before they can be used. This prevents developers from accidentally creating global variables, which can cause naming conflicts and make code harder to maintain. In non-strict mode, undeclared variables are automatically assigned to the global object, which can lead to unexpected behavior.
  • Function invocation: In non-strict mode, this keyword inside a function that is not invoked as a method of an object refers to the global object. In strict mode, however, this keyword is undefined in such cases. This helps prevent accidental modification of the global object and makes it easier to write secure code.
  • Function arguments: In non-strict mode, it is possible to declare multiple function parameters with the same name, which can lead to confusion and errors. Strict mode disallows this, making it easier to write and maintain code.
  • Deleting properties: In non-strict mode, it is possible to delete properties from objects that are not configurable. This can lead to unexpected behavior and should be avoided. In strict mode, attempting to delete non-configurable properties will result in an error.

Conclusion: In conclusion, strict mode in JavaScript provides better error checking and enforces stricter coding rules, leading to fewer bugs and better code quality. While non-strict mode may seem more flexible at first, it can lead to unexpected behavior and security vulnerabilities. It is therefore recommended to use strict mode in all JavaScript code to ensure the highest level of security and reliability.



Similar Reads

JavaScript Strict Mode
JavaScript is a dynamic language i.e. to elaborate, each Javascript component is dynamic starting from variables to the code itself. With the help of JavaScript, you can create variables in the runtime, change it's data-type, create new functions or replace existing logic. In other words, while using JavaScript the developer is in almost full contr
4 min read
What is Strict Mode and how to enable it in JavaScript ?
The strict mode of JavaScript, introduced in ECMAScript 5, is a mechanism to opt into a restricted version of JavaScript." Strict mode modifies the regular JavaScript semantics in the following ways: Some JavaScript silent errors are eliminated by converting them to throw errors. How to enable strict mode in JavaScript: Use "use strict" in front of
3 min read
JavaScript SyntaxError – Octal literals are not allowed in strict mode
If you put an octal literal in a code block that runs on strict, the result may be SyntaxError: Octal literals are not allowed in strict mode, since they can lead to unintended actions and confusion, numbers such as 0123 should never be strictly used, to understand this error better including possible causes, consider these examples. Understanding
3 min read
Strict mode in JavaScript
The "use strict" directive, introduced in ECMAScript version 5, is a literal expression rather than a statement and is ignored by earlier JavaScript versions. Its purpose is to enforce strict mode, ensuring the code is executed with stricter parsing and error handling. In strict mode, you cannot use undeclared variables, among other enforced rules.
5 min read
What is JavaScript Strict mode and how can we enable it ?
JavaScript is a forgiving language as it ignores developers' or programmers' silly mistakes or errors in code like termination of the statement, variable declaration, the wrong data type of variable, hoisting issues, and many more. Sometimes these errors give unusual results which difficult for programmers or developers to find an error in long cod
5 min read
New Features of strict Mode in React 18
React 18 introduces significant enhancements, especially in strict mode, aimed at improving development experiences. It offers comprehensive warnings to catch common mistakes early, along with improved component stack traces for easier debugging. These enhancements emphasize early error detection and smoother debugging processes, contributing to a
6 min read
JavaScript SyntaxError - "use strict" not allowed in function with non-simple parameters
This JavaScript exception 'use strict' not allowed in function occurs if strict modes 'use strict' statement is used at the beginning of a function with default parameters, rest parameters, or destructuring parameters. Message: Edge: Cannot apply strict mode on functions with non-simple parameter list Firefox: SyntaxError: "use strict" not allowed
1 min read
Difference between standards mode and quirks mode
Standards mode and Quirks mode are two modes introduced by web browsers to treat new standards-compliance sites differently from old legacy sites. At present, there are three modes used by the layout engines of web browsers to work along: quirks mode, almost standards mode, and full standards mode. The document type declaration of HTML, also known
3 min read
Strict Equality(===) Comparison Operator in JavaScript
JavaScript Strict Equality Operator is used to compare two operands and return true if both the value and type of operands are the same. Since type conversion is not done, so even if the value stored in operands is the same but their type is different the operation will return false. Syntax: a===b Example 1: In this example, we will compare the val
2 min read
Strict Inequality(!==) Comparison Operator in JavaScript
JavaScript Strict Inequality Operator is used to compare two operators and return true if they both are unequal. It is the opposite of the Strict Equality operator but like the Strict Equality Operator, it also does not perform type conversion. Syntax: a!==b Example 1: In this example, we will use the inequality operator on the same data types. C/C
1 min read
three90RightbarBannerImg