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.
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 .
Latest articles
- 03.04.24IT / Уроки PHP Уроки простыми словами. Урок 3. Все операторы PHP с примерами, с выводом работы кода на экран.
- 02.04.24IT / Уроки PHP Уроки простыми словами. Урок 2. Типы данных в PHP с примерами.
- 02.04.24IT / Уроки PHP Уроки простыми словами. Урок 1. Коротко о языке веб-программирования PHP. Основы синтаксиса.
- 09.11.23IT / Database Errors when migrating from MySQL 5.6 to 5.7 and how to fix them - database dump import failed with an error or INSERT does not work. Disabling STRICT_TRANS_TABLES strict mode or using IGNORE
- 08.07.22IT / Misc Convert office files DOC, DOCX, DOCM, RTF to DOCX, DOCM, DOC, RTF, PDF, HTML, XML, TXT formats without loss and markup changes