PASSWORD RESET

Your destination for complete Tech news

What does use strict do in javascript?

430 0
< 1 min read

The "use strict" directive is a way to opt in to a restricted variant of JavaScript. It enables strict mode, which is a way to write more secure and efficient code by eliminating certain syntax and behaviors that are potentially error-prone or confusing.

When strict mode is enabled, JavaScript will throw more exceptions and will be less tolerant of certain syntax errors and behaviors that are allowed in non-strict mode. For example, in strict mode, it is not allowed to use variables or functions that have not been declared, or to delete variables or functions that are not deletable. Strict mode also disables certain features that are deprecated or potentially dangerous, such as the eval function and the with statement.

To enable strict mode in JavaScript, you can put the "use strict" directive at the beginning of a script or a function. Here is an example:

"use strict";

function doSomething() {
  // code goes here
}

The "use strict" directive must appear at the top of the script or function, before any other statements. It can also be placed inside an IIFE (Immediately Invoked Function Expression) to enable strict mode for a specific block of code:

(function() {
  "use strict";
  // code goes here
})();

Enabling strict mode is optional, but it is generally a good practice to use it in production code to help prevent common coding mistakes and to ensure that your code is as secure and efficient as possible.

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.