JavaScript | Strict Mode

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 or 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 using 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. These set of restrictions made the code much more secure and maintains 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 the 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 keyword as variable names: Unlike most of the other programs JavaScript allowed to use 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 of the other programs JavaScript allowed to use duplicate parameter names which is disalloed in the strict mode.
    Source:

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

    Output:

    Uncaught SyntaxError: Unexpected eval or arguments in strict mode.

References:



My Personal Notes arrow_drop_up

FullStack Developer of six websites Verified Freelancer with 19 complete projects and 4.9 rating Modest Lifelong Learner

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.