Advantages of using strict mode:
- The strict mode eliminates silent errors by changing them into thrown errors. so, it makes debugging easier
- When the delete operator is used where it is invalid strict mode throws errors.
- When the variables are used without being declared or we call them “accidental globals”, strict mode throws an error.
- It doesn’t allow duplicate property names or parameter values.
- .When we do not use strict mode and our “this” operator points reference to null or undefined values it doesn’t give an error but enabling the strict mode raises an error.
- Code written in strict mode can sometimes be made to execute quicker than code written in a non-strict manner.
The strict mode doesn’t allow the following instructions:
- We cannot directly use variables without declaring the variable.
- Parameters/arguments duplicates are not allowed.
- Deletion function not allowed.
- The words ‘eval’ and ‘arguments’ cannot be used as a variable.
- The ‘with’ statement is not allowed.
- Deleting a property that is undeletable.
- Writing to a read-only property.
Demonstration without using the strict mode: In the below example we are assigning a variable without declaring it and then printing it.
Demonstration with using the strict mode: In the below example we are assigning a variable without declaring it and then printing it. we enable strict mode at the top of the code. The error message is displayed as we use strict mode.