Javascript Strict Mode
Posted by Superadmin on May 03 2023 02:47:05

Javascript Strict Mode

Javascript Strict Mode

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:

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;

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:

Javascript Strict Mode output 1

Example #2 – Function level strict mode in action

Code:

"use strict";
myFunc();
function myFunc() {
y = 1;   // error raised
}

Output:

Javascript Strict Mode output 2

Example #3 – Declaring strict within a function

Code:

x = 1;    // not raise error
myFunction();
function myFunction() {
"use strict";
y = 1;  // error raised
}

Output:

Javascript Strict Mode output 3

Example #4 – Initializing undefined object

Code:

"use strict";
x = {y:1, z:2};   // error raised

Output:

Javascript Strict Mode output 4

Example #5 – Deleting a variable

Code:

"use strict";
var x = 1;
delete x;     // error raised

Output:

Javascript Strict Mode output 5

Example #6 – Deleting a function

Code:

"use strict";
function x(y, z) {};
delete x;        // error raised

Output:

Javascript Strict Mode output 6

Example #7 – Duplicating a parameter

Code:

"use strict";
function x(y, y) {};   // error raised

Output:

Javascript Strict Mode output 7

Example #8 – Using octal number literals

Code:

"use strict";
var v = 011;   // error raised

Output:

Javascript Strict Mode output 8

Example #9 – Using octal escape characters

Code:

"use strict";
var x = "\011";   // error raised

Output:

Javascript Strict Mode output 9

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:

Javascript Strict Mode output 10

 

Example #11 – Modifying the get-only property

Code:

"use strict";
var obj = {get y() {return 0} };
obj.y = 1;   // error raised

Output:

Javascript Strict Mode output 11

Example #12 – Deleting an object prototype

Code:

"use strict";
delete Object.prototype;   // error raised

Output:

Javascript Strict Mode output 12

Example #13 – Initializing variable as ‘eval’

Code:

"use strict";
var eval = 1;   // error raised

Output:

output 13

 

Example #14 – Initializing variable as ‘arguments’

Code:

"use strict";
var arguments = 1;   // error raised

Output:

output 14

Example #15 – Using ‘with’ statement’

Code:

"use strict";
with (Math){y = sin(2)};   // error raised

Output:

output 15

Example #16 – Using ‘eval’ to create variables within the scope

Code:

"use strict";
eval ("var y = 1");
alert (y);      // error raised

Output:

output 16

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:

output 18.1

Code:

"use strict";
var implements= 1;   // error raised

Output:

output 18.2

Code:

"use strict";
var interface= 1;   // error raised

Output:

output 18.3

Code:

"use strict";
var let= 1;   // error raised

Output:

output 18.4

Code:

"use strict";
var package= 1;   // error raised

Output:

output 18.5

Code:

"use strict";
var private= 1;   // error raised

Output:

output 18.6

Code:

"use strict";
var protected= 1;   // error raised

Output:

output 18.7

Code:

"use strict";
var static= 1;   // error raised

Output:

output 18.8

Code:

"use strict";
var yield= 1;   // error raised

Output:

Javascript Strict Mode output 18.9

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.