Use Strict in JavaScript

Photo by Jexo on Unsplash

Use Strict in JavaScript

"use strict" is a JavaScript directive that indicates that the code should be executed in "strict mode." In strict mode, certain errors are thrown that would otherwise be ignored, and certain JavaScript features are disabled to make the code easier to write and to prevent accidental errors.

You can enable strict mode for an entire JavaScript file by adding the "use strict" directive at the top of the file, or for a specific function by adding it at the top of the function.

  • Declaring a variable before using it is required.

  • Declaring an object before using it is required.

  • The function cannot be deleted.

  • Duplicating a parameter name is not allowed.

  • Octal numeric literals are not allowed.

  • Octal escape characters are not allowed.

  • Modifying a read-only property is not permitted.

  • Modifying a get-only property is not permitted.

  • Deleting an undeletable property is not allowed.

  • The use of the reserved word "eval" as a variable is not permitted.

  • The use of the reserved word "arguments" as a variable is not permitted.

  • The with statement is not allowed.

  • For security reasons, using eval() to create variables in the scope from which it was called is not permitted.

  • In strict mode, eval() can not declare a variable using the var or let keyword.

It is a good practice to use strict mode because it can help you avoid mistakes and make your code more robust and maintainable.