×

Strict Mode In JavaScript

It is new feature included in ES5. It is used to execute the code in "strict mode". we can achieve this by adding 'use strict' in the beginning of your script or function. only the code below the assignment comes under strict mode.

Declaring in the beginning of your script makes it available globally in the code (inside the function as well). Declaring inside the function make it available locally inside the function.

##Strict mode helps out in a couple ways:

List of features

x = 20;
var undefined = 5; // throws a TypeError var Infinity = 5; // throws a TypeError
var obj = {}; Object.defineProperty(obj, "x", { value: 0, writable: false }); obj.x = 3.14; // This will cause an error
var obj = { get x() { return 0; }, }; obj.x = 3.14; // This will cause an error
delete obj; //SyntaxError: Delete of an unqualified identifier in strict mode
delete Object.prototype; //throws error
var x = { x1: "1", x1: "2" }; //throws error
function sum (x, x) {...}//throw error
var x = 010; // This will cause an error
with (Math) { x = cos(2); } // This will cause an error
eval("var x = 2");
var x; delete x;

###SOME EXAMPLES/ OBSERVATIONS

function func() { "use strict"; y = 100; //this will not throw an error till the function is not called }
"use strict"; function func() { y = 100; //this will not throw an error till the function is not called }
function func() { myGlobal = 5; //this will NOT give error } (function () { "use strict"; func(); })(); function func_strict() { "use strict"; myGlobal = 5; //this will give error }

We hope that this post is helpful for you. If really, Please rate us and let us know your review in the comment.

that's it my dear devs :)

If you like it please give a 👍

Thank you,
CapsCode
www.capscode.in