JavaScript Strict Mode – Part 1


JavaScript Strict Mode – Part 1

JavaScript Strict Mode - Part 1









The ‘use strict’ directive is new JavaScript 1.8.5 (ECMAScript version 5). It is not a statement, but a literal expression, ignored by earlier versions of JavaScript. The purpose of “use strict” is to indicate that the code should be executed in “strict mode”. With strict mode, you cannot, for example, use undeclared variables.

Strict mode is declared by adding “use strict”; to the beginning of a JavaScript or a JavaScript function.

Declared at the beginning of a JavaScript file, it has 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).


Advantage of using Strict Mode

  • Strict mode makes it easier to write “secure” JavaScript.
  • Strict mode changes previously accepted “bad syntax” into real errors.
  • As an example, in normal JavaScript, mistyping a variable names creates a new global variable. In strict mode, this will throw an error, making it impossible to accidently create a global variable.

n normal JavaScript, a developer will not receive any error feedback assigning values to non-writable properties. In strict mode, any assignment to a non-writable property, a getter-only property, a non-existing property, a non-existing variable, or a non-existing object, will throw an error.

Below are the code example show the use of strict mode.


This is allowed in JavaScript without strict mode

x = 3;

document.write("Value of x=" + x);


This code will not give any error, as it is ignored by JavaScript, but if use strict mode for same then it will throw an error.




If you open developer console in your browser you can see exact line of code where error is thrown.




But if we declare ‘x’, it will not throw any error.

"use strict"

var x = 3;

document.write("Value of x=" + x);


Deleting a variable

Deleting a variable is not allowed in strict mode.

"use strict"

var x = 3;

document.write("Value of x=" + x);

delete x;


Even visual studio will show alert message when you call delete on an expression.



Deletion is allowed only on objects which are defined.

"use strict"

var car = { carName: "Lancer", color: "Red" };


delete car.carName;


Deleting a function is not permissible in strict mode.

"use strict"

function test() {

var x = 5;



delete test;




Hope you’ve enjoyed this post. Keep visiting for more updates.



JavaScript, ASP.Net & PHP Web Developer. Connect with me on Facebook and Twitter.

Share This Post

Related Articles

Powered by Paras Babbar · Designed by Paras Babbar