JavaScript

What is strict mode in JavaScript?

In this tutorial, we are going to see What is strict mode in JavaScript?

The ECMA 5 standard brings many new features to the JavaScript language. These include the ability to enable a strict mode to improve your code and make it cleaner.

The strict mode can be enabled by simply writing the string “use strict” in a JavaScript file. The strict mode is then valid for the entire file. You can also restrict this mode to a function by writing the directive in the function’s code.
 

 

Example : strict mode in JavaScript
/* Apply the "strict mode" to the entire file */
use strict;

(function(){
 use strict;
 //"strict mode" enable in this function
 //...
})();

The strict mode allows detecting problems in your code. Exceptions are then thrown. If potentially dangerous actions, such as accessing a global variable, are performed, the strict mode can prevent them or issue an error. It also disables features that are more commonly used or confusing for the developer. It also detects the use of the eval keyword, reserved by the language to interpret code. All recent browsers can use the strict mode. For Internet Explorer, it is only compatible with version 10 and earlier, but since it is a simple string, it will not generate an error if it is read by an older browser.
 

 

What are the advantages?

This feature brings three major advantages:

  • Turn some silent Javascript errors into visible and blocking errors to improve the maintainability of the code.
  • Allow the Javascript engine to perform more optimizations at runtime, so it is possible that the code is sometimes faster.
  • Prevent the use of certain keywords that will be used in future versions of the ECMAScript standard.
mcqMCQPractice competitive and technical Multiple Choice Questions and Answers (MCQs) with simple and logical explanations to prepare for tests and interviews.Read More

Leave a Reply

Your email address will not be published. Required fields are marked *