JavaScript Use Strict

JavaScript

"use strict"; Defines that JavaScript code should be executed in “strict mode” only i.e.don’t use undeclared variables.Strict mode makes it easier to write “secure” JavaScript.

Declaring Strict Mode:

Strict mode is declared by adding “use strict”; to the beginning of a JavaScript file, or a JavaScript function and it will have a global scope (all code will execute in strict mode). Declared inside a function, it has local scope (only the code inside the function is in strict mode).

Global declaration:

(function() {
  'use strict';
  n = 2; // will give error because n has not been declared
function example() {
 x = 3; // will also give error because x has not been declared
}


})();

Local declaration:

(function() {
 
n =2; // no error
function example() {
 'use strict';
x= 3; // will give error because x has not been declared
}


})();

Leave a Reply

Your email address will not be published. Required fields are marked *