'use strict' in JavaScript

This 'use strict' directive has been introduced in ECMA 5.It defines that javascript code should be executed in strict mode.
Strict mode is declared by adding 'use strict'  to the head of a script or function.If it is declared in beginning of a script,It has global scope.
It has some advantages,
  • It returns some common errors and exception.
  • It prevents some unsecured action such as it won't give any access to use global variables or object.
Now we see some example to understand use strict mode

 "use strict";  
 y = 3.14;  // it will cause an error because y is not declared  
 var y = 3.14; // it doesn't make any error because y was declared  

Like variable,if we use object without any declaration it will cause an error.strict mode actually not allows bad syntax.In javascript,if we make an misspelling in variable name creates a new global variable.This type of mistakes will not happened in strict mode.

What are the thinks not allowed in strict mode? 
  • Without declaration we cannot able to use variable and object.
  • Deleting a variable,object and function is not allowed.
  • Escape characters like \t or \b and Octal numeric letters are not allowed.
  •  read only and get only property are not allowed.
  • implements,interface,let,package,private,protected,public,static and yield are also not allowed.


Comments