Cyber Week

Save big!

All courses under $5 - for a limited time!

Code has been added to clipboard!

Best Way of Using JavaScript use strict Statement in a Nutshell

Reading time 5 min
Published Aug 8, 2017
Updated Oct 2, 2019

When writing JavaScript code, there are various problems you may run into. One of the things that can help you avoid bugs is JavaScript strict mode that you can turn on using JavaScript use strict directive.

Writing JavaScript in strict mode allows you to avoid possible bugs that you may run into if you accidentally deviate from the correct JavaScript syntax. Strict mode is also useful in cases of typos, which can result in unnecessary new variables.

JavaScript Use Strict: Main Tips

  • By using JS use strict directive, you set JavaScript code to be executed in strict mode.
  • This directive will be recognized depending on where it was written. It can be the whole document or a particular function.

What is JS Use Strict?

This directive was newly introduced in ECMAScript5. Thus, it is ignored by earlier JavaScript versions. It is a literal expression, not a statement.

The purpose of the use strict JavaScript directive is to make the code execute in strict mode. This mode sets up certain conditions for the execution of code to make it cleaner. For example, with strict mode, you cannot use undeclared variables.

Keep in mind that strict mode isn't universally supported. These are the first versions of the major browsers that support this directive (the later ones support it, too):

  • IE version 10
  • Firefox version 4
  • Chrome version 13
  • Safari version 6
  • Opera version 12.1

Strict Syntax

Take a look at how your should enter the strict mode in JavaScript:

"use strict";

The syntax used for declaring strict mode is compatible with older JavaScript versions. However, it doesn't work in them. For example, when compiling number literals (1 + 2;) or string literals ("John Johnson";) JavaScript will not run into any problems. It will only compile to a variable that does not exist and die.

So, JS use strict directive will only be read by the new compilers correctly. The older ones simply won't recognize it.

Note: make sure not to forget quotation marks and the semicolon at the end: "use strict"; .

Reasons to Use

This way of writing JavaScript may prove to be more secure than usual. It turns certain cases of bad syntax into actual errors, making your code cleaner and less likely to contain bugs. For example, if you mistype a variable name in JavaScript, it will create a new global variable. When using strict mode, an error will be thrown, making it impossible to create a new global variable by accident.

In JavaScript, developers usually do not receive any error feedback when assigning values to non-writable properties. In strict mode, you cannot assign anything to non-writable properties, get-only properties, non-existing properties, variables, or objects, for an error will be thrown straightaway.

Declaring Strict Mode

You can declare strict mode by using the JS use strict directive at the beginning of a file or a function definition.

If declared at the beginning of a file, all of the code will execute in strict mode:

Example
"use strict";
a = 2 + 2;
Example
"use strict";
sampleFunction();  
function sampleFunction() {
   b = 2+2;
}

If declared at the beginning of a function definition, a function form of use strict will be generated. It has a local scope, only applying to the block of code describing the function, and not outside of it:

Example
a = 2 + 2;
sampleFunction();
function sampleFunction() {
     "use strict";
     b = 2 + 2;
}

What's Not Allowed

Let's go throught the list of what you cannot do when JavaScript is in strict mode. First of all, you cannot use undeclared variables:

Example
"use strict";  
a = 2 + 2;

You cannot use undeclared objects, too:

Example
"use strict"; 
a = {
  p1: 11,
  p2: 22,
};

Variables, objects, or functions cannot be deleted using the delete keyword:

Example
"use strict";
var a = 2 + 2;  
delete a;
Example
"use strict";
function a(p1, p2) {}; 
delete a;

You cannot duplicate a parameter name:

Example
"use strict";
function a(p1, p1) {};

You cannot use octal numeric literals:

Example
"use strict";
var a = 01;

Escape characters are not allowed too:

Example
"use strict";
var a = \01;

Writing to a read-only or get-only property is not allowed:

Example
"use strict";
var objec = {};  
Object.defineProperty(objec, "a", { value: 1, writable: false });  
objec.a = 2;
Example
"use strict";
var objec = {get a() { return 0 } };
objec.a = 2;

You cannot delete undeletable properties:

Example
"use strict";
delete Object.prototype;

You cannot use strings "eval" or "arguments" as variables:

Example
"use strict";
var eval = 2; // causes an error
Example
"use strict";
var arguments = 2; // causes an error

You cannot use with statement:

Example
"use strict";
with (Math) { a = cos(2) };

For security reasons, you cannot use eval() for creating variables inside the scope it was called from. In a function call like f(), the value of this was the global object. In strict mode, it is undefined:

Example
"use strict";
eval ("var a = 2");  
alert (a);

Future Proof Keywords

You may remember some words cannot be used to call new functions or variables. They are called reserved words. Usually, they are unavailable because they are used for inbuilt functions or other language elements already.

Now, some keywords are reserved for future JavaScript versions. Thus, they cannot be used in strict mode as well. Let's take a look at the list:

  • interface
  • implements
  • package
  • let
  • protected
  • private
  • static
  • yield
  • public
Example
"use strict";
var public = 1400;

JavaScript Use Strict: Summary

  • The use strict directive executes JavaScript code in strict mode.
  • You have to be aware of where you place the use strict JavaScript directive, since it will give different results. For example, placing it within a function will create the function form of use strict.
  • There are specific rules you must follow when writing code in strict mode.