Sign in Registration
ArticlesDownloadServicesAudioContacts
ruen

What is use strict mode in JavaScript ES5 and why you should use it

JavaScript initially allows you to write code in a fairly simple mode, which can lead to various implicit errors and security issues . With the emergence of new standards and specifications (ECMAScript 5 (ES5) 2009) for this language, such problems are intended to be solved by a more strict regime - use strict.

chto-takoe-rezhim-use-strict

use strict mode switches the JavaScript code processor to advanced code review mode. Some of the written constructions may start giving errors after enabling this mode. This mode may not be supported by some browsers, including older ones (IE 9–) - they treat the directive as an ordinary string and do not make any sense. Enabling the mode will allow you to find errors faster and prevent the use of incorrect coding techniques.

How do I enable use strict ? By default, the mode is disabled for compatibility with the old code, it is recommended to always enable this mode in your projects. To switch the code to modern mode, it is enough to write at the beginning of the file with JS code:

"use strict" ;

or

'use strict' ;

It is also possible to use this mode inside functions, then it will only apply to the code inside the current block. However, it is easier to specify the directive for the entire file. The directive must be specified at the very beginning of the file or function, otherwise it will not work (it can be placed after comments). After enabling the mode, it cannot be disabled for the current section or file. It is important to remember that if using the the console in browser to test your code , you must also enable mode in a similar way if you want to test your code in strict mode.

Above it was found out why use use strict - it is recommended to use it everywhere, but in the future it is possible to automatically enable strict mode by default. For example, already now you can use new JS structures - classes and modules in which this mode is enabled by default. The advent of strict mode will allow JS to improve in the future.

Enabling strict mode imposes a number of restrictions, in particular, it is not allowed in the new mode:

- duplicate parameter name ;

- using variable and objects without declaration ;

- deleting a variable, objects, functions, properties that cannot be deleted ;

- use of octal numeric literals (010) and escape characters ("\ 010") ;

- write to read-only or get-only property ;

- use words eval, arguments ;

- with declaration ;

- create variables with eval in the scope from which it was called .

When writing code, it is also important to remember the new reserved words and not use them in your code: publicv , static , let , package , private , implements , interface , yield , protected .

Comments (0)
For commenting sign in or register.

Latest articles

Popular sections

Communication

Login to the web version
Android app:
Available on Google Play

Share this

Subscribe to