Sign in
Log inSign up
All About " Strict Mode " in JavaScript

All About " Strict Mode " in JavaScript

Maanil Verma's photo
Maanil Verma
·May 26, 2021·

3 min read

Hi, welcome to my new blog where I will make you understand the importance of the " use strict " mode in JavaScript.

Since you know JavaScript is a scripting language therefore it shows the correct output even if the script has some errors in it.

Now, to solve this problem in ECMAScript 5, a feature called " use strict "; [ Strict Mode ] was introduced.

" use strict "; expression is used to enable the Strict Mode in JavaScript.

Let's discuss more about the Strict Mode feature in detail !!

What is Strict Mode?

Strict Mode is a new feature in ECMAScript 5 which allows you to write a program or function in the Strict operating context. It prevents the program to execute if it contains some bugs or errors and shows the exceptions.

Why do we need Strict Mode?

  • Prevents Syntax Mistakes by throwing errors.

  • Prevents from errors that go unnoticed by throwing exceptions.

  • Prevents from taking illegal actions in JavaScript like accessing the global object.

  • Helps browser in Optimizations which otherwise are difficult to make.

How do you declare strict mode?

Strict Mode should always be written on the top of the script using " use strict " or ' use strict '.

// Here Strict Mode is activated.

" use strict ";
let myName = " Maanil ";
console.log(myName);    // Output: Maanil

// Here Strict Mode is not activated as it is ignored

let myName = " Maanil ";
console.log(myName);    // Output: Maanil

" use strict ";  // Here strict mode gets neglected by the JavaScript.

This is how we declare strict mode right at the top of Script.

Using Strict Mode with Variables

  • When we declare " use strict " in the Script
//When "use strict" is declared at the top it has Global Scope.

"use srict";

myName = "Maanil";
console.log(myName);

// It will throw a Reference Error as we have not defined the variable.
  • Without "use strict"
myName = "Maanil";
console.log(myName); // Output: Maanil

In the above case, without Strict Mode it executes without error as JavaScript automatically defines it as variable and Strict Mode is used to eradicate these types of errors.

Using Strict Mode with Functions

Strict Mode in Functions has local scope if "use strict" is defined inside the function.

firstNumber = 3.14;       // This will not cause an error.
pieFunction();

function pieFunction() {
  "use strict";
  secondNumber = 3.14;   // This will cause an error as "use strict" has local scope.
}

Mostly Strict Mode is used when we are involved in the process of Web Development.

Summary

I hope this article has clarified all your misconceptions about the Strict Mode. Before I summarize the learnings, let me tell you one thing that this "Strict Mode" comes with caution and a developer should know when to use it and when to not.

I will discuss about cautions of using Strict Mode in some other article. For now, let's jot down the learnings:

  • Strict Mode is a new feature in ECMAScript 5 which allows you to write a program or function in the Strict operating context.

  • " use strict "; expression is used to enable the Strict Mode in JavaScript.

  • Strict Mode should always be written on the top of the script.

  • When "use strict" is declared at the top it has Global Scope.

  • Strict Mode in Functions has local scope if "use strict" is defined inside the function.

  • Mostly Strict Mode is used when we are involved in the process of Web Development.

That's it from this blog, I hope we will meet again in some other blog.

Till then keep JavaScripting and Stay Tuned !!