Latest Code Tutorials

Javascript Strict Mode: How to Use Strict Mode in JS

JavaScript’s strict mode was introduced in ECMAScript 5, which is the way to opt into the restricted variant of JavaScript. JavaScript (JS) is a lightweight interpreted or JIT-compiled programming language with first-class functions. JavaScript is a prototype-based, multi-paradigm, dynamic language, supporting object-oriented, imperative, and declarative (e.g., functional programming) styles.

Javascript Strict Mode

Strict mode makes the following changes to standard JavaScript language.

  1. It eliminates some JavaScript silent errors by changing them to throw errors to the user so that they can interpret easily.
  2. It fixes the mistakes that make it difficult for the JavaScript engines to perform the optimizations/ The strict mode code can sometimes be made to run faster than the identical code that’s not the strict mode.
  3. It prohibits some syntax likely to be defined in future versions of ECMAScript.

The “use strict” statement was new in ES5. We can also define with a single quotation like ‘use strict.’

It is the literal expression, ignored by earlier versions of the JavaScript.

The purpose of “use strict” literal is to indicate that the code should be executed in  the “strict mode.” So we can identify some hidden bugs.

With the strict mode, you can not use undeclared variables.

All the modern browsers support “use strict” except IE 9 and lower.

Invoking Javascript Strict Mode

A strict mode in Javascript applies to entire scripts or the individual functions. It doesn’t apply to the block statements enclosed in {} braces; attempting to use it to such contexts does nothing.

Now, If we want to invoke the strict mode for an entire script, put the exact statement “use strict.” See the following example.

// app.js

'use strict';

const student = { name: 'KRUNAL', education: 'BE IT' };
const freezedObj = Object.freeze(student); = 'ANKIT';

In the above code, we have a frozen object. Then we have tried to change that object, so if we try to change the frozen object, then it should throw an error. Let’s run the above file.


Javascript Strict Mode Tutorial

So, due to the use strict statement, it has thrown an error saying that we can not assign the value to the read-only property.

Now, if you remove the “use strict” statement and then again run the file, you will not see the exception.

// app.js

const student = { name: 'KRUNAL', education: 'BE IT' };
const freezedObj = Object.freeze(student); = 'ANKIT';


Javascript Strict Mode

It does not change the student object, but it also does not throw an error saying that we can not change the student object because it had frozen. So we can not say easily what is going on here.

So, “use strict” statement has eliminated some JavaScript silent errors by changing them to throw errors. It is quite useful when we are debugging the Javascript Application.

Strict mode for scripts

If we need to invoke strict mode for an entire script, put the exact statement “use strict.” Let’s see the following example.

// app.js

"use strict"

v = "Hi! I'm a strict mode script!";


In the above code, we have not defined the variable; we have directly assigned the value to that variable. We have also added the “use strict” statement. So it should give us an error saying that we have not defined a variable.


Invoking Javascript Strict Mode

Now, if you remove the “use strict” statement from the code, then it will usually run and gives the output.

Strict mode for Javascript Functions

We can use the “use strict” statement inside the Javascript Function. See the following example.

// app.js

x = 21

function myFunction() {
  "use strict"
  y = 21

In the above code,  we have added the “use strict” statement inside the function. We have defined two variables without declaring it. But, the scope of “use strict” is bound to function and not the whole script file.

So, x=21 will not give us an exception, but inside the function, we have used the “use strict” statement.

So, when the function runs, it will throw an error of y variable because we have not declared yet.


Javascript Strict Mode Tutorial With Example

Why Strict Mode?

The strict mode makes it easier to write “secure” JavaScript.

The strict mode changes previously accepted “bad syntax” into real errors.

As an example, in regular JavaScript, mistyping the variable name creates the new global variable. In the strict mode, it will throw an error, making it impossible to create the global variable accidentally.

In standard JavaScript, the developer will not receive any error feedback assigning values to the non-writable properties.

In strict mode, any assignment to the non-writable property, a getter-only property, a non-existing property, the non-existing variable, or the non-existing object will throw an error.

Keywords reserved for future JavaScript versions cannot be used as the variable names in strict mode. Those keywords are the following.

  1. implements
  2. interface
  3. let
  4. package
  5. private
  6. protected
  7. public
  8. static
  9. yield

For the future, when you use a browser console to test features, please note that it doesn’t “use strict” by default. We need to add manually.

Finally, the Javascript Strict Mode Example is over.

  1. Younus says

    This article provides a walkthrough for creating a Node.js application that … Exit focus mode. Contents. … Send events to Azure Event Hubs using Node.js. 02/19/2019;

  2. Amit Parmar says

    thanks for the post

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.