Curriculum
Course: JavaScript Basic
Login

Curriculum

JavaScript Basic

JSHome

0/216
Text lesson

JS Strict Mode

The “use strict” Directive

The “use strict” directive was introduced in ECMAScript 5 and is not a statement, but rather a literal expression that earlier JavaScript versions ignore.

Its purpose is to enforce “strict mode,” which imposes stricter parsing and error handling on the code. For example, in strict mode, undeclared variables cannot be used. All modern browsers support “use strict,” except for Internet Explorer 9 and earlier versions.

js 3

The numbers in the table indicate the first browser version that fully supports the directive.

You can apply strict mode in all your programs to write cleaner code, such as preventing the use of undeclared variables. Since “use strict” is just a string, Internet Explorer 9 will not throw an error even if it doesn’t recognize it.

Declaring Strict Mode

Strict mode is activated by placing “use strict”; at the beginning of a script or function. When declared at the start of a script, it applies globally, meaning all code within the script will run in strict mode.

Example

“use strict”;
x = 3.14;       // This will cause an error because x is not declared

Example

“use strict”;
myFunction();

function myFunction() {
  y = 3.14;   // This will also cause an error because y is not declared
}

When declared inside a function, strict mode has a local scope, meaning only the code within that function will be executed in strict mode.

x = 3.14;       // This will not cause an error.
myFunction();

function myFunction() {
  “use strict”;
  y = 3.14;   // This will cause an error
}

The “use strict”; Syntax

The syntax for declaring strict mode was designed to be backward-compatible with older versions of JavaScript. For example, compiling a numeric literal (4 + 5;) or a string literal (“John Doe”;) in a JavaScript program has no side effects; it simply results in a non-existent variable and does nothing. Therefore, “use strict”; only affects newer compilers that recognize its meaning.

Why Strict Mode?

Strict mode helps make JavaScript code more secure by turning previously accepted “bad syntax” into actual errors.

For example, in regular JavaScript, mistyping a variable name creates a new global variable, but in strict mode, this results in an error, preventing the accidental creation of global variables.

Additionally, in regular JavaScript, developers do not receive error feedback when assigning values to non-writable properties. However, in strict mode, any attempt to assign a value to a non-writable property, a getter-only property, a non-existent property, a non-declared variable, or an undefined object will throw an error.

Not Allowed in Strict Mode

Using a variable without declaring it is not permitted.

“use strict”;
x = 3.14;                // This will cause an error

Objects are also considered variables.

Using an object without declaring it is not allowed.

“use strict”;
x = {p1:10, p2:20};      // This will cause an error

Deleting a variable (or object) is not permitted.

“use strict”;
let x = 3.14;
delete x;                // This will cause an error

Deleting a function is not allowed.

“use strict”;
function x(p1, p2) {};
delete x;                // This will cause an error 

Duplicating a parameter name is not permitted.

“use strict”;
function x(p1, p1) {};   // This will cause an error

Octal numeric literals are prohibited.

“use strict”;
let x = 010;             // This will cause an error

Octal escape characters are not permitted.

“use strict”;
let x = “\010”;            // This will cause an error

Writing to a read-only property is not permitted.

“use strict”;
const obj = {};
Object.defineProperty(obj, “x”, {value:0, writable:false});

obj.x = 3.14;            // This will cause an error

Writing to a getter-only property is not allowed.

“use strict”;
const obj = {get x() {return 0} };

obj.x = 3.14;            // This will cause an error

Deleting a non-deletable property is not permitted.

“use strict”;
delete Object.prototype// This will cause an error

The word eval cannot be used as a variable name.

“use strict”;
let eval = 3.14;         // This will cause an error

The word arguments cannot be used as a variable name.

“use strict”;
let arguments = 3.14;    // This will cause an error

The with statement is not allowed.

“use strict”;
with (Math){x = cos(2)}; // This will cause an error

For security reasons, eval() is not allowed to create variables in the scope from which it is called.

In strict mode, a variable cannot be used before it is declared.

“use strict”;
eval (“x = 2”);
alert (x);      // This will cause an error

In strict mode, eval() is not allowed to declare a variable using the var keyword.

“use strict”;
eval (“var x = 2”);
alert (x);    // This will cause an error

eval() is not allowed to declare a variable using the let keyword in strict mode.

eval (“let x = 2”);
alert (x);        // This will cause an error

In strict mode, the behavior of the this keyword in functions differs.

When a function is called, this typically refers to the object that invoked the function.

If no object is specified, functions in strict mode will set this to undefined, whereas in non-strict mode, this will default to the global object (such as window in browsers).

“use strict”;
function myFunction() {
  alert(this); // will alert “undefined”
}
myFunction();

Future Proof!

In strict mode, you cannot use keywords reserved for future JavaScript versions as variable names. These include:

  • implements
  • interface
  • let
  • package
  • private
  • protected
  • public
  • static
  • yield
“use strict”;
let public = 1500;      // This will cause an error