"use strict";
It is an literal expression. The purpose of "use strict" is to indicate that the JavaScript code should be executed in "strict mode".
With strict mode, you cannot use undeclared variables.
i.e. Compiling a numeric literal (2 + 5;) or a string literal ("Arun Kumar";) in a JavaScript program has no side effects. It simply compiles to a non existing variable and dies.
Declaration and Scope of "use strict"
It is declared by adding "use strict" at the beginning of a JavaScript file or a JavaScript function. Its effects are similar to global and local variables in javascript.
Declared at the beginning of a JavaScript file, it has global scope means all code will execute in strict mode.
Declared inside a function, it has local scope i.e. only the code inside the function is in strict mode.
Global declaration:
"use strict";
x = 3.14; // This will cause an error
myFunction(); // This will also cause an error
function myFunction() {
x = 3.14;
}
Local declaration:
x = 3.14; // This will not cause an error.
myFunction(); // This will cause an error
function myFunction() {
"use strict";
x = 3.14;
}
Expressions that are Not Allowed in Strict Mode
1) Using a variable (property or object) without declaring it, is not allowed:
"use strict";
x = 3.14; // This will cause an error
// (if x has not been declared)
2) Deleting a variable, a function, or an argument, is not allowed.
"use strict"; x = 3.14; delete x; // This will cause an error3) Defining a property more than once, is not allowed:
"use strict";
var x = {p1:10, p1:20}; // This will cause an error
4) Duplicating a parameter name is not allowed:
"use strict";
function x(p1, p1) {}; // This will cause an error
5) Octal numeric literals and escape characters are not allowed:
"use strict"; var x = 010; // This will cause an error var y = \010; // This will cause an error6) Writing to a read-only property is not allowed:
"use strict";
var obj = {};
obj.defineProperty(obj, "x", {value:0, writable:false});
obj.x = 3.14; // This will cause an error
7) Writing to a get-only property is not allowed:
"use strict";
var obj = {get x() {return 0} };
obj.x = 3.14; // This will cause an error
8) Deleting an undeletable property is not allowed:
"use strict"; delete Object.prototype; // This will cause an error9) The string "eval" cannot be used as a variable:
"use strict"; var eval = 3.14; // This will cause an error10) The string "arguments" cannot be used as a variable:
"use strict"; var arguments = 3.14; // This will cause an error11) The with statement is not allowed:
"use strict";
with (Math){x = cos(2)}; // This will cause an error
12) For security reasons, eval() are not allowed to create variables in the scope from which it was called:
"use strict";
eval ("var x = 2");
alert (x) // This will cause an error
Conclusion :
I hope that this article would have helped you in understanding the use of Use Strict in JavaScript. Your feedback and constructive contributions are always welcome.


No comments:
Post a Comment