Open In App

JavaScript Strict Mode

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Share
Report issue
Report

JavaScript is a dynamic language i.e. to elaborate, each Javascript component is dynamic starting from variables to the code itself. With the help of JavaScript, you can create variables in the runtime, change it’s data-type, create new functions or replace existing logic. In other words, while using JavaScript the developer is in almost full control. Why Almost? There is a famous saying,

“With great power comes great responsibility.”Ben Parker

JavaScript follows this principle quite dearly, while in a marksman coder’s project JavaScript performs like one of the best Programming Languages out there, it may work totally randomly in the hands of a novice. This random behavior while being an integral part to build the desire to learn JavaScript inside-out can build up certain complexities, especially if used in a project. Developers of JavaScript added a new feature in ES5 known as the strict-mode, which is supposed to disallow certain behaviors of the language to decrease the random behavior and increase the detectability of poorly written code. This set of restrictions made the code much more secure and maintained a high standard of coding in general. JavaScript codes are optimized before execution by the engine, using the strict mode it was seen that developers could now write highly optimized programs. It is not only recommended by developers but also a mandatory inclusion of industrial coding standards.

Syntax: In order to use strict mode in your script we just need to exercise the following line below, strict-mode also known as strict mode pragma has its own scope and can affect the whole file or individual methods depending on the same.

"use strict";

Functionalities: We know now that strict mode is basically a mode of JavaScript that is much more obsessed with correct syntax and other logical paradigms that it used to allow without much probing. But, what are these syntaxial and logical errors that are not allowed in the strict mode anymore? The following is a brief list of a few important ones.

Auto-Global Variable Declaration: This is one of the biggest problems in JavaScript, without using the strict mode if you mistakenly use a variable without its definition, JavaScript doesn’t throw an error instead it declares the variable in global scope which often leads to randomness and undesired outputs. With strict mode enabled it will throw a regular reference error notifying that the variable to be used was never defined. 

Source:

"use strict"; // Turn on strict mode.
a = 1;  

Output:

Uncaught ReferenceError: a is not defined
  • Note: In JavaScript objects are variables as well thus it also requires the keyword ‘var’, ‘let’, or ‘const’ to define one.
  • Deletion of any JavaScript element: This is a big change from the regular mode as in the strict mode it is disallowed to delete any variable or function. This makes the code much more optimizable as scopes are static and don’t change over a lifetime. 

Source:

"use strict"; // Turn on strict mode.
var a = 1;
delete a;  

Output:

Uncaught SyntaxError: Delete of an unqualified identifier in strict mode.
  • Note: You may ask why does the error say unqualified? JavaScript provides the functionality where you can define a property of an object as deletable that qualifies the property to be deleted in strict mode.
  • Using reserved keywords as variable names: Unlike most other languages, JavaScript allows the use of reserved keywords as the variable names which are disallowed in the strict mode. 

Source:

"use strict"; // Turn on strict mode.
var eval = 5; 

Output:

Uncaught SyntaxError: Unexpected eval or arguments in strict mode.

Duplication of parameter names: Unlike most other programming languages, JavaScript allows the use of duplicate parameter names which is disallowed in the strict mode. Source:

Example:

Javascript




<script>
    // Normal mode
    function myFunc(a, a){
        console.log(a);
    }
     
    myFunc(0, 10);
</script>


Output:

10

Example:

Javascript




<script>
    // Strict mode
    "use strict"; // Turn on strict mode.
     
    function myFunc(a, a){
    console.log(a);
    }
     
    myFunc(0, 10);
</script>


Output:

Uncaught SyntaxError: Duplicate parameter name not allowed in this context

To know more about Strict mode please go through this article



Last Updated : 07 Nov, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads