“use strict” is an expression stating that JavaScript code should be executed in “strict mode.” The expression was introduced in ECMAScript 5.
“Strict mode” is a more restricted version of JavaScript, where semantics are altered to make the code more resilient and secure. In “strict mode,” some silent errors are changed to throw errors and disable some of the more confusing or undefined features in JavaScript.
For example, you can not use undeclared variables in strict mode :
"use strict"a=1;
The above code throws an error while the code below executes :
a=1;console.log(a);
Another restriction can be seen in the example below where the code throws an error as the strict mode does not allow you to delete a variable. Try removing the "use strict"
statement and it will work!
"use strict"a=1;delete a;
Also, note that “use strict” has global scope when written at the beginning of the code while it will have a local scope if written inside a function body.
Here is an example of “use strict” in local scope:
a=1;//this will not throw an error as "use strict" is in local scopefunction strictMode() {"use strict"alert("I am in strict mode");}