Posted by : at

Category : programming   javascript

What is Strict Mode and What it does ?

  • Strict mode removes functionality that was possible in ES3, and deprecated since ES5. It will throw error, if you do so.
'use strict';

Putting the above line first in a file, program or a function switches on the so-called strict mode. It changes the semantics of the JS language, It’s really important to know the main differences between he Javascript code in strict mode, and normal Javascript, which is often referred to as sloppy mode.

  • Accidental Global variables

    If you assign a value to an undeclared variable, Javascript by default creates that variable on the global object.

    But with strict mode, you can not, an error is raised. for example, use undeclared variables.

'use strict';

age = 10;

// Assuming age is not declared.
// Throws ReferenceError: age is not defined

  • Function Arguments with Same Name

Strict mode requires that all parameters to a function are unique

'use strict';

function onLogin (id , first_name, first_name) {  // SYNTAX ERROR
   return first_name + second_name;
  • Errors for immutables

In Strict mode, assignments to non-writable or getting-only property or creating a new property on a non-extensible object will throw an error.

'use strict';

var xobj = {};
Object.defineProperty(xobj, 'x', { value: "Script Name", writable: false });
xobj.x = "New Name";  // TYPEERROR

var yobj = { get y() { return "Name"; } };
yobj.y = "New Name"; // TYPEERROR

var fixedobj = {};
Object.prventExtensions(fixedobj); = "Name"; // TYPEERROR

  • Eval Scope

In Normal Code eval(“var x;”) introduces a variable x into the surrounding function or the global scope.But in strict mode, any variable or functions created inside of eval() stay inside of eval(). You can, however return a value from eval() if you wish to pass a value back out:

'use strict';

var sum = eval("var x = 30, y = 40; x + y");

// Works in strict and non-strict mode

// Strict Mode, throws an error because x is undeclared
  • Eliminates this coercion

In Strict Mode, the value of this will not be auto-coerced to an object. If the first argument to call or apply is null or undefined, the this value of the invoked function will not be converted to the global object.

'use strict'; = "sujay";
funtion testFunction() {

// Throws an error in strict mode, "sujay" otherwise 

// Throws an error in strict mode, "sujay" otherwise;

  • Undeletable Properties

Deleting undeletable properties and functions throws error

'use strict';

// Deleting Object.prototype
delete Object.prototype; // TYPEERROR

// Deleting a variable, a function, or an argument will result in an error.

var name = "test";
function testFunction(){}
 delete name; // SyntaxError 
 delete testFunction; // SyntaxError

function testFunction2(arg){
   delete arg; // SyntaxError

  • Prohibits with

The strict mode eliminates the with statement. It is now considered invalid Javascript Syntax and will throw a syntax errow when it appears in strict mode code.

'use strict';

// Causes a syntax error in strict mode
var obj = {name: 'sujay'};
with (obj) {
  • Prohibits arguments.callee

In strict mode, arguments.callee is considered invalid Javascript Syntax.

'use strict';

// Causes a syntax error in strict mode
function(testInt) {
  if(testInt-- == 0)

  • Prohibits Octal Syntax

Strict mode prohibits octal: Assigning an octal value to a numeric literal or attempting to use an escape on an octal value.

'use strict';

var x = 010 + 100; // SYNTAXERROR due to octal variable 010
var testescape = \010; // SYNTAXERRIR

Some more strict checks are encompassed in strict mode, such as you cannot use eval, arguments as an identifier (variable or function name, parameter name, and so on). It blocks certain future possible ECMAScript keywords from being used such as implements, interface, package, private, protected, public, static, and yield.

Always “use strict” where and when possible. This will avoid your code from running into all sorts of problems, as one would know that maintenance of the code is the most difficult task rather than writing new code.

That’s All folks ! Thanks for reading this article. I know you are deeply interested in learning JS. So I will be coming up with cool stuff like this in Future.

Don’t forget to Subscribe via Email ! That’s Important. So that you get my articles directly via Email.

And Thanks Again. Keep Visiting ! :D