Modern standard “use strict” in JavaScript

JavaScript “use strict”

Modern standard “use strict” in JavaScript tutorial from Coding compiler. For a very long time, the JavaScript language developed without losing compatibility. New features were added to the language, but the old – never changed, so as not to “break” the already existing HTML / JS-pages with their use.

Strict mode in JavaScript

However, this led to the fact that any error in the design of the language became “frozen in” it forever.

This was the case before the emergence of the ECMAScript 5 (ES5) standard, which simultaneously added new features and introduced a number of corrections to the language, which may lead to the fact that the old code that was written before it appeared would stop working.

To prevent this from happening, we decided that by default these dangerous changes will be turned off, and the code will work as before. And in order to translate the code into full compliance with the modern standard, you need to specify a special directive use strict.

This directive is not supported by IE9-.

Directive use strict

The directive looks like a string “use strict”;or ‘use strict’;and is placed at the beginning of the script.

For example:

“use strict”;

// this code will work according to the modern ES5 standard

Cancel the action use strict can not

There is no directive no use strictor the like that returns to the old mode.

If we are already in modern mode, then this is a one-way road.

use strict for functions

After a while we will pass the function.

For the future, we note that use strict, you can also specify at the beginning of the functions, then strict mode will act only inside the function.

In the following chapters, we will elaborate on the differences in the work of the language with use strict and without it.

Do I need to use strict?

If we talk in the abstract, then – yes, we need it. In strict mode, some errors in the design of the language have been corrected, and in general, the modern standard is good.

However, there are two problems.Support for IE9- browsers that are ignored “use strict”.

Suppose that we, using “use strict”, developed the code and tested it in the Chrome browser. Everything works … However, the probability of errors at the same time in IE9- has grown! It always works according to the old standard, which means, sometimes in a different way. The errors will have to debug already in IE9-, and it is much less pleasant than in Chrome.

However, the problem is not so terrible. Incompatibilities are few. And if you know them (and in the textbook we dwell on them) and write the correct code, then everything will be fine and “use strict”will become our loyal assistant.Libraries written without regard to “use strict”.

Some libraries that are written without “use strict”, do not always work correctly, if the calling code contains “use strict”.

First of all, it refers to third-party libraries that we did not write, and which I would not like to rewrite or edit.

There are few such libraries, but when translating long-existing projects to “use strict”this problem arises with enviable regularity.

Conclusion

Write code with use strict should be only if you are sure that the problems described above will not.

Of course, all the code that is in this tutorial works correctly in mode “use strict”.

ES5-shim

IE8 browser supports only the very old version of the JavaScript standard, namely, ES3.

Fortunately, many features of the modern standard can be added to this browser by connecting the ES5 shim library , namely, scripts to es5-shim.js and es5-sham.js from it.

Conclusion

In this chapter, we introduced the concept of “strict regime”.

Further, we will assume that the development is carried out either in a modern browser, or in IE8 with the connected ES5 shim . This will allow us to use most of the features of modern JavaScript in all browsers.

Very soon, literally in the next chapter, we will see the features of strict mode with concrete examples.

Related JavaScript Tutorials For Beginners

JavaScript Introduction Tutorial
Introduction to JavaScript
Javascript Reference and Specifications
Javascript Code editors
Javascript Developer Console
JavaScript Basics Tutorial
JavaScript Hello World.!
External JavaScript Files
JavaScript Code Structure


Leave a Reply

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