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" ;
'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 .
- 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
- 09.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
- 07.07.22IT / Safety How to protect PHP, JS, HTML, CSS source code - obfuscation, minification, compression and encryption
- 06.07.22IT / Safety Connection not secure, problem with Lets Encrypt - how to fix expired 09/30/2021 DST Root CA X3, remove it manually and install ISRG Root X1. Example on MS Windows 7
- 08.07.21IT / Misc How to make a free translation for a website without an API, translate documents in Google Translate