Explanation of Strict Mode in JavaScript

Explanation of Strict Mode in JavaScript

Hello everyone, in this article we are going to talk about strict mode in Javascript we will see the benefits and importance of strict mode in Javascript of strict mode.

Lets get started

Before we start we should know that Javascript is everywhere on the internet and it is providing us powerful structures to handle the operations easily. However javascript has no type safety definitions and users can input various types of data. So we need to make it dynamic and allow to handle the expected data types. This might make the debugging a bit difficult. In here strict mode support us and with strict mode mode we can debug the issues easily.

What is this "use strict"?

"Use strict" is a command which is introduced with EcmaScript5 to enable the strict mode and error handling in JavaScript. We use the "use strict" at the beginnin gof the script or function, and after that code will be executed within the strict mode. With it we can see the errors in the log to understand the issues better

Advantages of Strict Mode
  • Performance Optimisations In strict mode some optimisations can be applied during the execution and it leads to faster execution during the operation.
  • Error catching and handling In strict mode, we can see the runtime errors during the execution of the applicaiton. Whenever an error happened, in strict mode an exception will be throwed and we will see that in logs. We can also understand and fix the unexpected behaviours during the execution with the help of error catching.
  • Security Strict mode prevents the insecure or deprecated features, this makes the code more secure and reliable.
  • Always Compatible With strict mode, our code will be ready automatically of the changing and developing of Javascript. So it will help us to migrate the new versions of the code.

Now lets take a look at the common errors that get caught by strict mode in Java Script

Strict mode can caught the invalid assigning. When we assign a value to wrong data type or an undeclared variable, Strict mode will cath it.

"use strict";
mistakeVar = 10; 
Deleting an object with delete operator. As you know delete operator only delete the referances not the objects, So, Strict mode will catch the delete operator with an onject and throw error

delete Object.prototype;
We can not use any reserved keywords for a variable or function names. So, in strict mode these kind of usages will be caught.

var let = 10; //Strict mode will throw error due to variable name

Now lets take a look at the usages of Strict Mode in Javascript

To enable strict mode in java script we need to enable it with use strict first in the related scope.


"use strict";

When we enable it at the top of the entire script it will be enabled for all the scope of the script.


"use strict"

var a = "Thecodeprogram";
a = 10; // Error will be throwed in here

function test() {
     const b = "burak";
     b = true; // Error will be thrown in here too
}

test();

We can enable strict mode for a function. To enable it in a function scope we need to call use strict in the relevant function.


var a = "Thecodeprogram";
a = 10; 

function myFnc() {
     "use strict" //Enable strict mode in current function scope

     const b = "burak";
     b = true; // Will caughted by strict mode 
}

That is all for this article.

Burak Hamdi Tufan


Tags


Share this Post

Send with Whatsapp

Post a Comment

Success! Your comment sent to post. It will be showed after confirmation.
Error! There was an error sending your comment. Check your inputs!

Comments

  • There is no comment. Be the owner of first comment...