In this post we will have a look at one of the most important Javascript feature called as “strict mode”. We normally execute Javascript code in normal mode which allows many things which are actually of wrong syntax and unsecure. But when we use strict mode, Javascript code is executed without ignoring wrong syntax and also make the code more secure..
“use strict” directive
To execute our code in strict mode we need to use the “use strict” directive. Its just a expression. This directive is supported by JavaScript 1.8.5(ECMAScript 5) or above.
Using “use strict” directive
There are two ways of using “use strict” directive. We can use it as function declaration or global declaration.
When we use “use strict” as global declaration, all the javascript code in the page is executed in strict mode.
For example:
"use strict"
function print(val){
console.log(val);
}
function print(val){
console.log(val);
}
When we use “use strict” as function declaration, all the code inside the function is executed as strict mode. All other code outside the function is executed as normal mode.
For example:
function print(val){
"use strict"
console.log(val);
}
function print_twice(val){
console.log(val);
console.log(val);
}
"use strict"
console.log(val);
}
function print_twice(val){
console.log(val);
console.log(val);
}
Things that are not allowed in strict mode are:
Use of undeclared variable
In strict mode we cannot use an undeclared variable.
"use strict"
a = 12; //this throws exception in strict mode. In non-strict mode a new property for window object is created.
a = 12; //this throws exception in strict mode. In non-strict mode a new property for window object is created.
delete operator
Delete operator is used to delete user defined object properties and array elements. If we try to delete anything other than user defined object properties or array elements we get an exception.
"use strict"
var a = 78;
window.b = 34;
function print(){}
window.c = function(){}
delete a; //throws exception in strict mode. In non strict mode it returns false and program continues. 'a' is treated as a global variable not as an property of window object in both modes, so property operations are not allowed in variable a.
delete window.a; //throws exception in strict mode. In non strict mode it returns false and program continues.
delete window.b; //In both modes property b is deleted.
delete Math.PI; //In strict mode it returns false as PI is pre defined. In strict mode exception is thrown.
delete Object.prototype; //deleting an undeletable property in non-strict mode returns false. But in strict mode exception is thrown.
var a = 78;
window.b = 34;
function print(){}
window.c = function(){}
delete a; //throws exception in strict mode. In non strict mode it returns false and program continues. 'a' is treated as a global variable not as an property of window object in both modes, so property operations are not allowed in variable a.
delete window.a; //throws exception in strict mode. In non strict mode it returns false and program continues.
delete window.b; //In both modes property b is deleted.
delete Math.PI; //In strict mode it returns false as PI is pre defined. In strict mode exception is thrown.
delete Object.prototype; //deleting an undeletable property in non-strict mode returns false. But in strict mode exception is thrown.
To learn more about delete operator click here. Just remember that wherever delete returns false in non-strict mode it throws exception in strict mode.
Multiple same declaration of property
In strict mode you cannot declare same object properties more than once. Otherwise it will throw exception.
var myObject = {property1:1, property2:2, property1:1}; //throws an exception.
If you declare global variables or functions more than once than no exception is thrown. Its only applicable for object properties.
Duplicate parameter names
In strict mode you cannot declare same parameter name more than once.
function print(value, value) {} //throws exception.
Octal numeric literals and escape characters are not allowed
Octal numeric literals and escape characters are not allowed in strict mode.
var testStrict = 010; // throws exception
var testStrict = \010; // throws exception.
var testStrict = \010; // throws exception.
read-only and get-only Property
Writing to read-only and get-only properties of an object throws exception.
var testObjone = {};
Object.defineProperties(testObjone,"x",{value:0,writable:false});
testObjone.x = 3.14; // exception throws.
var testObjtwo = {get x() {return 0} };
testObjtwo.x = 3.14; // exception thrown.
Object.defineProperties(testObjone,"x",{value:0,writable:false});
testObjone.x = 3.14; // exception throws.
var testObjtwo = {get x() {return 0} };
testObjtwo.x = 3.14; // exception thrown.
“eval” and “arguments”
We cannot use eval and arguments strings as variable, property or object names in strict mode.
var eval = 12; //In non-strict mode eval is overwritten. Exception thrown in strict mode.
var arguments = 12;//Same behavior as above.
var arguments = 12;//Same behavior as above.
“with” statement
Use of “with” is not allowed in strict mode.
with (Math){x = sin(2)}; // throws an exception.
To learn more about with statement click here.
Reserved keywords
In strict mode we cannot use some words which are reserved for future version of JavaScript.
Reserved keywords are:
- implements
- interface
- package
- private
- protected
- public
- static
- yield
"use strict"
var private = 12; //in non-strict mode this is allowed. In strict mode this throws an exception.
var private = 12; //in non-strict mode this is allowed. In strict mode this throws an exception.
“this” keyword
“this” keyword inside global functions is undefined in strict mode.
"use strict"
function checkforthis(){
console.log(this); //will print undefined.
}
checkforthis();
function checkforthis(){
console.log(this); //will print undefined.
}
checkforthis();
eval creating variables
eval function creates variables in the scope from which it was called. But in strict mode it doesn’t.
"use strict"
eval("var q = 12");
console.log(q); //throws exception. q is not defined.
eval("var q = 12");
console.log(q); //throws exception. q is not defined.
Benefits of strict mode
- Strict mode changes previously accepted “bad syntax” into real errors.
- In JavaScript mistyping a variable name creates a new global variable. In strict mode, this will throw an error, making it impossible to accidentally create a property of window variable.
- In normal JavaScript, a developer will not receive any error feedback assigning values to non-writable properties. And also any assignment to a non-writable property, a getter-only property, a non-existing property, a non-existing variable, or a non-existing object, will throw an error
Conclusion
We from above theory we can say that strict mode helps us to reduce typing mistakes, common programming errors and also makes our code compatible for future versions of browsers. If you like this post please “Like and Share”.
No comments:
Write comments