Updated April 11, 2023
Introduction to Javascript Strict Mode
Strict Mode is a new feature being introduced in JavaScript with the advent of ES5 changes. It changes the whole schema of ordinary code in JavaScript and makes it more of a “strict” operating category. The Strict context makes the JavaScript code more able to deny certain actions and throw more exceptions. This mode can be implemented within the code by writing “use strict” which invokes the browser to operate in the strict mode which is a safer and better mode to work in. Some browsers which are not supporting the strict mode will behave differently when strict mode is used inside them hence a prior feasibility testing of the browser should be also done before working in a strict mode. Strict mode code and no-script mode code can co-occur at the same time in the code hence the scripts can opt to strict mode slowly and incrementally.
Syntax
We can state any JavaScript code to be used in Strict Mode by just using a sentence “use strict” in supported browsers as follows:
'use strict';
var x = 4; //Valid in strict mode
y = 1; //invalid in strict mode
Benefits of Using Strict Mode
Strict mode offers multiple changes to a normal JavaScript code changing its semantics and making it more controlled via error throwing logic. Some of the main benefits of its introduction in JavaScript are mentioned below:
- The Strict mode help eliminate some of the silent errors in JavaScript by raising a throw error statement in such cases.
- The Strict Mode throws, or prevent errors, whenever relatively “unsafe” actions are being taken within the JavaScript Code (i.e. accessing a global object or initializing a variable without defining it).
- The Strict mode basically fixes faults in the strict context that makes it easier for the JavaScript engine to carry out optimizations. Hence, strict mode JavaScript code can erratically be running quicker than the same JavaScript code that is running in non-strict mode.
- Some of the syntax and semantics are likely to be added in JavaScript with future versions of ECMAScript hence it would be easier if the foundation of those changes is prohibited by Strict Mode.
- The Strict Mode can be invoked in scripts, functions, and modules as well.
Strict Mode in Scripts:
'use strict';
var x = 'Strict Mode in Scripts!';
Strict Mode in Functions:
const Strict = () => {
'use strict';
return "Strict Mode in Functions!"
}
Strict mode in Modules:
function method( ) {
// being a module it is by default strict in nature
}
export default method;
- The Strict mode basically helps writing strict and secure JavaScript code.
- The Strict Mode has disabled features that were confusing or poorly thought out.
Examples of Javascript Strict Mode
Here are the following examples mention below
Example #1 – Initializing an undefined variable
Code:
"use strict"
var v = 1 // error raised
Output:
Example #2 – Function level strict mode in action
Code:
"use strict";
myFunc();
function myFunc() {
y = 1; // error raised
}
Output:
Example #3 – Declaring strict within a function
Code:
x = 1; // not raise error
myFunction();
function myFunction() {
"use strict";
y = 1; // error raised
}
Output:
Example #4 – Initializing undefined object
Code:
"use strict";
x = {y:1, z:2}; // error raised
Output:
Example #5 – Deleting a variable
Code:
"use strict";
var x = 1;
delete x; // error raised
Output:
Example #6 – Deleting a function
Code:
"use strict";
function x(y, z) {};
delete x; // error raised
Output:
Example #7 – Duplicating a parameter
Code:
"use strict";
function x(y, y) {}; // error raised
Output:
Example #8 – Using octal number literals
Code:
"use strict";
var v = 011; // error raised
Output:
Example #9 – Using octal escape characters
Code:
"use strict";
var x = "\011"; // error raised
Output:
Example #10 – Modifying the read-only property
Code:
"use strict";
var obj = {};
Object.defineProperty(obj, "y", {value:1, writable:false});
obj.y = 10; // error raise
Output:
Example #11 – Modifying the get-only property
Code:
"use strict";
var obj = {get y() {return 0} };
obj.y = 1; // error raised
Output:
Example #12 – Deleting an object prototype
Code:
"use strict";
delete Object.prototype; // error raised
Output:
Example #13 – Initializing variable as ‘eval’
Code:
"use strict";
var eval = 1; // error raised
Output:
Example #14 – Initializing variable as ‘arguments’
Code:
"use strict";
var arguments = 1; // error raised
Output:
Example #15 – Using ‘with’ statement’
Code:
"use strict";
with (Math){y = sin(2)}; // error raised
Output:
Example #16 – Using ‘eval’ to create variables within the scope
Code:
"use strict";
eval ("var y = 1");
alert (y); // error raised
Output:
Example #17 – Use of ‘this’ keyword in functions of strict mode
The ‘this’ keyword is used for referring the object that called some function. If any object is undefined in a function then in strict mode, the function will return ‘undefined’ for the object and functions working in non-strict mode will rather return the global value of the object (if defined globally).
Code:
"use strict";
function myFunc() {
alert(this);
}
myFunc();
Example #18 – Using reserved words of future JavaScript versions for defining a variable
Code:
"use strict";
var public = 1; // error raised
Output:
Code:
"use strict";
var implements= 1; // error raised
Output:
Code:
"use strict";
var interface= 1; // error raised
Output:
Code:
"use strict";
var let= 1; // error raised
Output:
Code:
"use strict";
var package= 1; // error raised
Output:
Code:
"use strict";
var private= 1; // error raised
Output:
Code:
"use strict";
var protected= 1; // error raised
Output:
Code:
"use strict";
var static= 1; // error raised
Output:
Code:
"use strict";
var yield= 1; // error raised
Output:
Conclusion
Strict Mode is an exciting feature introduced with ECMAScript 5.0 which has enhanced the semantics and also added extra secureness to the JavaScript coding platform. It has decreased the fault tolerance in JavaScript and also makes it easy for the developers to write code in JavaScript without worrying about some of the common errors which are explicitly thrown by it.
Recommended Articles
This is a guide to Javascript Strict Mode. Here we discuss the Examples of Javascript Strict Mode along with the benefits. You may also have a look at the following articles to learn more –