Everything You Need To Know About "use Strict" In JavaScript

Ā·

3 min read

Everything You Need To Know About "use Strict" In JavaScript

Hey, Phenomenal peoplešŸ‘‹šŸ»

Hello everyone, my name is Aryan Sharma, and I hope you're all doing well. I'm thrilled to begin this JavaScript course for absolute beginners with my third blog post.

You can find my blogs hereā†—ļø

So, Let's dive!!

ā€œuse strictā€

JavaScript has developed without any compatibility problems for a very long period. The language now has additional capabilities while maintaining its previous functionality.

This situation continued up until the release of ECMAScript 5 (ES5) in 2009. It changed some of the previous characteristics and added new ones to the language.

Most of these improvements are disabled by default to preserve the old code functioning. The specific directive "use strict" must be used to expressly enable them.


When "use strict" or 'use strict' is located at the top of a script, the whole script works the ā€œmodernā€ way.

"use strict";

Your JavaScript code here

Ensure that ā€œuse strictā€ is at the top.

šŸš«Please make sure that "use strict" is at the top of your scripts, otherwise strict mode might not be enabled.

Strict mode isnā€™t enabled here:

alert("Code");
// "use strict" below is ignored

"use strict";

// strict mode is not enabled

Also, Keep in your mind, Thereā€™s no way to cancel use strict

There is no such directive like "no use strict" so to undo the effect of use strict

Once we enter strict mode, thereā€™s no going back.

Now, you might be thinking why add use script ? or what are the benefits of adding use script in our JS code?šŸ¤”

Why use "use state"

One could recommend starting scripts with "use strict"ā€¦ But you know why it is important to do so....?

  1. Strict mode variables: In strict mode, variables must be declared before they are used. This prevents the accidental creation of global variables by omitting the var, let, or const keyword.

  2. Eliminating silent errors: In strict mode, certain coding practices that can lead to silent errors are treated as explicit errors. For example, assigning values to read-only properties, deleting variables, or duplicating parameters.

  3. Strict mode for functions: When "use strict" is applied to a function, that function and its nested functions operate in strict mode. If you're using modules, each module is implicitly in strict mode.

  4. Changes to the "this" value: In strict mode, the value this inside a function is not automatically coerced to the global object (e.g., window in browsers) or to undefined in strict mode functions that are not methods. Instead, it remains the value it was set to when the function was called.

  5. Octal literals and escape sequences: Octal literals (e.g., 0123) and certain escape sequences (e.g., \012) are not allowed in strict mode, as they were a source of confusion and potential errors.

    A strict mode is a useful tool for catching and preventing common programming mistakes. It encourages cleaner and more reliable JavaScript code by eliminating certain language features that are prone to errors or considered bad practices.

    meme from supermeme or tenor

I Hope you loved the blogā£ļø

See u in the next blog...Stay tunedšŸŽ¶

  1. Don't forget to follow me on:
    Twitter LinkedIn
Ā