“Strict mode” is a feature in JavaScript that enables a stricter version of the JavaScript language, with additional checks and warnings to help you write safer and more robust code. When you use strict mode, certain language features that can be problematic or confusing in certain situations are disabled, and certain other behaviors are changed to be more consistent or to throw errors when unexpected conditions occur.
You can enable strict mode for an entire script by adding the string literal “use strict” at the beginning of the script, or for a specific function by adding the same string literal at the beginning of the function.
Example of Strict Mode
Here’s an example of how you might use strict mode in a script:
"use strict";
let x = 5;
let y = 10;
let sum = function(x, y) {
return x + y;
};
console.log(sum(x, y)); // Output: 15
In this example, the script will be executed in strict mode because of the “use strict” directive. In strict mode, this script will raise error if you try to use undeclared variable, and some other features that can be problematic or confusing in certain situations are disabled.
It is recommended to use strict mode in production code as it helps to write safer and more robust code.
Advantages of Strict Mode in JavaScript
Strict mode in JavaScript has several advantages:
Preventing accidental global variables: In normal JavaScript, if you accidentally assign a value to an undeclared variable, JavaScript will automatically create a global variable with that name. In strict mode, this behavior is prevented and an error will be thrown instead.
Disabling ‘with’ statement: The ‘with’ statement can make code harder to understand and maintain, and can also cause performance issues. In strict mode, the ‘with’ statement is disabled.
Making ‘eval’ safer: The ‘eval’ function can be used to execute arbitrary code, which can be a security risk. In strict mode, the ‘eval’ function is slightly modified to be safer.
Preventing duplicate property names: In normal JavaScript, you can create an object with properties that have the same name. In strict mode, this behavior is prevented and an error will be thrown instead.
Making octal literals prohibited: Octal literals are a feature that can be confusing and hard to understand. In strict mode, octal literals are not allowed.
Making ‘arguments’ and ‘caller’ properties non-configurable: In normal JavaScript, the ‘arguments’ and ‘caller’ properties can be deleted, which can cause unexpected behavior. In strict mode, these properties are non-configurable, which means that they cannot be deleted.
Making ‘arguments’ and ‘caller’ properties non-writable: In normal JavaScript, the ‘arguments’ and ‘caller’ properties can be overwritten, which can cause unexpected behavior. In strict mode, these properties are non-writable, which means that they cannot be overwritten.
Making ‘delete’ operator more strict: In normal JavaScript, the ‘delete’ operator can be used to delete properties from objects, even if the properties are non-configurable. In strict mode, the ‘delete’ operator is more strict and will throw an error if the property is non-configurable.
Also Read:
- What Is a Web Worker In JavaScript?
- How To Sort an Array of Strings In JavaScript?
- ECMAScript vs JavaScript
- Arrow Functions In JavaScript
- Difference Between Node.js and AngularJS With Example
- Difference Between == And === In JavaScript
- What Is JSON In JavaScript
- How To Go Back To Previous Page In JavaScript?
- How To Detect A Mobile Device With JavaScript?
- How To Close The Current Tab In A Browser Window Using JavaScript?
- How To Convert Input Text To Uppercase While Typing Using JavaScript?
- How To Show A Confirmation Message Before Delete In JavaScript?
- How To Detect Browser or Tab Closing In JavaScript?
- How To Get Hash Value From URL Using JavaScript?
- How To Get The Name, Size, And Type Of A File In JavaScript?
- Run JavaScript From The Command Line