Strict Mode is a new feature in ECMAScript 5 that allows you to place a program, or a function, in a “strict” operating context. This strict context prevents certain actions from being taken and throws more exceptions. The statement “use strict”; instructs the browser to use the Strict mode, which is a reduced and safer feature set of JavaScript.
Benifits of using ‘use strict’
Strict mode makes several changes to normal JavaScript semantics.
- Strict mode eliminates some JavaScript silent errors by changing them to throw errors.
- Strict mode fixes mistakes that make it difficult for JavaScript engines to perform optimizations: strict mode code can sometimes be made to run faster than identical code that’s not strict mode.
- Strict mode prohibits some syntax likely to be defined in future versions of ECMAScript.
- It prevents, or throws errors, when relatively “unsafe” actions are taken (such as gaining access to the global object).
- It disables features that are confusing or poorly thought out.
- Strict mode makes it easier to write “secure” JavaScript.
How to use strict mode
Strict mode can be used in two ways – used in global scope for the entire script and can be applied to individual functions. Strict mode doesn’t work with block statements enclosed in {} braces.
Using Strict mode for the entire script
To invoke strict mode for an entire script, put the exact statement “use strict”; (or ‘use strict’;) before any other statements.
// Whole-script strict mode syntax 'use strict'; var v = "strict mode script!";
NOTE: This syntax has a flow : it isn’t possible to blindly concatenate non-conflicting scripts. Consider concatenating a strict mode script with a non-strict mode script: the entire concatenation looks strict! The inverse is also true: non-strict plus strict looks non-strict. Concatenation of strict mode scripts with each other is fine, and concatenation of non-strict mode scripts is fine. Only concatenating strict and non-strict scripts is problematic. It is thus recommended that you enable strict mode on a function-by-function basis (at least during the transition period).
Using Strict mode for the entire script
Likewise, to invoke strict mode for a function, put the exact statement “use strict”; (or ‘use strict’;) in the function’s body before any other statements.
function strict() {
// Function-level strict mode syntax
'use strict';
function nested() { return 'Javascript on GeeksforGeeks'; }
return "strict mode function! " + nested();
}
function notStrict() { return "non strict function"; }
Examples of using Strict mode
// Using a variable, without declaring it, is not allowed: "use strict"; x = 3.14; // This will cause an error
Objects are variables too.
Using an object, without declaring it, is not allowed:
"use strict";
x = {p1:10, p2:20}; // This will cause an error
"use strict";
var x = 3.14;
Deleting a function is also not allowed
"use strict";
function x(p1, p2) {};
delete x; // This will cause an
"use strict";
function x(p1, p1) {}; // This will cause an error
"use strict"; var x = 010; // This will cause an error
"use strict"; var x = \010; // This will cause an error
"use strict";
var obj = {};
Object.defineProperty(obj, "x", {value:0, writable:false});
obj.x = 3.14; // This will cause an error
"use strict";
var obj = {get x() {return 0} };
obj.x = 3.14; // This will cause an error
"use strict"; delete Object.prototype; // This will cause an error
"use strict"; var eval = 3.14; // This will cause an error
"use strict"; var arguments = 3.14; // This will cause an error
"use strict";
with (Math){x = cos(2)}; // This will cause an error
"use strict";
eval ("var x = 2");
alert (x); // This will cause an error
Recommended Posts:
- JavaScript | Strict Mode
- this in JavaScript
- Map.has( ) In JavaScript
- Map.get( ) In JavaScript
- Map in JavaScript
- JavaScript | typedArray.some() with Example
- JavaScript | typedArray.set() with Example
- JavaScript | Generator
- JavaScript | weakMap.get()
- JavaScript JSON
- JavaScript | Callbacks
- JavaScript | Array pop()
- Atomics.xor() In JavaScript
- Debouncing in JavaScript
- JavaScript | escape()
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



