DEV Community

Yuta Kusuno
Yuta Kusuno

Posted on

[JS] What happens when “use strict” is defined

JavaScript's "use strict" directive allows you to opt into a restricted variant of the language, enabling a more robust and error-prone coding environment. When "use strict" is defined, several changes occur in the behavior of your code, enhancing its reliability. In ES Modules available from (ES6)ES2015, it is always in strict mode though. In this post, I will explore various scenarios illustrating the impact of strict mode.

Declaration Before Assignment

In strict mode, attempting to assign a value to a variable without prior declaration results in a ReferenceError. For instance:

"use strict";
exampleVar = 1; // ReferenceError: exampleVar is not defined
Enter fullscreen mode Exit fullscreen mode

Here, the absence of a declared variable exampleVar leads to a runtime error.

Restrictions on delete Operator

Strict mode imposes restrictions on the use of the delete operator with variables. An attempt to delete a variable declared with let raises a SyntaxError:

"use strict";
let exampleLet = 1;
delete exampleLet; // SyntaxError: Delete of an unqualified identifier in strict mode.
Enter fullscreen mode Exit fullscreen mode

Duplicate Parameter Names

Strict mode disallows the use of duplicate parameter names within function declarations:

"use strict";
function exampleFunc(param1, param1) {} // SyntaxError: Duplicate parameter name not allowed in this context
Enter fullscreen mode Exit fullscreen mode

This ensures parameter uniqueness and avoids potential confusion.

Octal Literals

Old-style octal literals are prohibited in strict mode. Instead, you should use the modern 0o prefix:

"use strict";
let oldOctal = 010; // SyntaxError: Octal literals are not allowed in strict mode.
let modernOctal = 0o10; // OK
Enter fullscreen mode Exit fullscreen mode

Read-Only Properties

In strict mode, attempts to modify read-only properties result in a TypeError. Consider the following example:

"use strict";
const object = {};
Object.defineProperty(object, 'propertyX', {
  value: 0,
  writable: false,
});

object.propertyX = 1; // TypeError: Cannot assign to read only property 'propertyX' of object '#<Object>'
Enter fullscreen mode Exit fullscreen mode

In this case, it sets writable: false, the result might be taken for granted. However, the TypeError does not occur without "use strict".

Reserved Keywords as Variable Names

Certain keywords such as private, package, and protected are reserved in strict mode and cannot be used as variable names:

"use strict";
var private = 1; // SyntaxError: Unexpected strict mode reserved word
Enter fullscreen mode Exit fullscreen mode

Prohibition of 'with' Statement

The with statement is forbidden in strict mode due to its potential for creating ambiguous code. Any attempt to use it will result in a SyntaxError:

"use strict";
let area;
const radius = 1;
with (Math) {
  area = PI * radius * radius;
} // SyntaxError: Strict mode code may not include a with statement
Enter fullscreen mode Exit fullscreen mode

Limited Visibility of eval Declarations

Declarations made within eval statements are not visible outside of the eval scope:

"use strict";
eval("var exampleVar = 1;");
console.log(exampleVar); // ReferenceError: x is not defined
Enter fullscreen mode Exit fullscreen mode

Here, exampleVar is only defined within the eval statement, causing an error when trying to access it externally.

That is about it. Happy coding!

Top comments (2)

Collapse
 
random_ti profile image
Random

Clear and concise breakdown!

Well done, Yuta🔥

Collapse
 
yutakusuno profile image
Yuta Kusuno

Thanks for the feedback, Random!