Use Strict in JavaScript | Strict mode

In this tute, we will discuss use strict in JavaScript. You can also call it the strict mode.

What is Use Strict?

The "use strict" is a JavaScript directive used to execute JavaScript code in strict mode.

If we run the script in strict mode, we cannot take the liberty of using undeclared variables, undeclared objects, and so on.

The main purpose of strict mode is to code secure JavaScript.


The strict mode is declared by using "use strict"; at the beginning of a script. The strict mode has its scope depending on where it is declared. The two scopes are:

  • Global scope
  • Local scope

When we declare the strict mode at the beginning of the entire script, its scope is global. When we declare the strict mode at the beginning of a function definition, its scope is local. Hence, depending on the need, we can declare the mode accordingly.

Global Scope:

In this mode, the strict mode applies to the entire script. Therefore, every statement of the script is executed in strict mode.

"use script";

var x = 10; // Accepted
y = 10;     // Error because y is not defined
function func() {
    n = 45; // Error

Local Scope:

In this mode, the strict mode applies only to the block in which it is declared. Therefore, statements outside the block are not executed in strict mode.

var x = 10; // Accepted
y = 10;     // Accepted

function func() {
    "use script";
    n = 45; // Error

Purpose of Strict Mode:

The strict mode shows errors for “bad syntax” statements which were previously accepted.

It helps to keep the code clean and make the script secure.

In normal mode, when a developer uses a new variable name by mistake, no error is shown. While in strict mode, this will throw an error.

Restrictions in Strict Mode:

Use of undeclared variable (or object) is not allowed

"use script";

var x = 10;                        // Allowed
y = 45;                            // Not allowed
z = {'car':'red', 'bike':'black'}  // Not allowed

Deleting a variable (or object) is not allowed.

"use script";

var x = 10;
delete x;                          // Causes an error

Deleting a function is not allowed.

"use script";

function x(var) {}
delete x;                          // Causes an error

Similarly, there are a few things that are not allowed in strict mode:

  • Using an undeclared variable/object.
  • Using eval to create variable/object.
  • Deleting a variable/object.
  • Deleting a function.
  • Using a parameter more than once in a function.
  • Using Octal numeric literals.
  • Usage of Octal escape characters.
  • Writing read-only property.
  • Writing get-only property.
  • Deleting undeletable property.

Furthermore, few words and keywords cannot be used as variable names in the strict mode:

  • eval
  • arguments
  • public
  • implements
  • static
  • package


By this time you would’ve probably understood the basic idea of strict mode. If you want to explore the strict mode, even more, try all your future scripts in strict mode and keep your code clean and secure.

Finally, If you have any queries or doubts related to use strict in JavaScript, simply comment in the comment section provided below.

Related posts:

Leave a Reply

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