Strict mode became available in ECMAScript 5. Using it allows us to prevent certain actions from being taken and to allow it to throw better exceptions. Strict mode is supported in all major browsers (Chrome, Edge, Firefox, Safari).
Strict Mode helps us write better quality JavaScript by instituting the following:
- It catching a lot of common coding errors
- It prevents by throwing errors when relatively “unsafe” actions are taken (such as gaining access to the global object).
- It disables features that are confusing or poorly thought out.
Use strict can be placed inside a function or be placed at the top of the file. Generally, you’ll want to do it at the start of the file but inside a function is okay too. Here’s an example that might be done until a full file can be updated.
Here’s an example at the top of a file: