How to Use Strict Mode in JavaScript?

Estimated read time 1 min read

In JavaScript, the “strict mode” is a way to opt in to a restricted variant of JavaScript. Strict mode makes it easier to write “secure” JavaScript.

You can enable strict mode for an entire script by including the following line at the top of your script:

"use strict";

You can also enable strict mode for a specific function by including the line inside the function:

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

When strict mode is in effect, it catches common coding mistakes and “unsafe” actions, and throws an error when such actions are encountered. For example:

  • Strict mode disallows using a variable without declaring it first with var, let, or const.
  • Strict mode disallows using a property of an object that has not yet been declared.
  • Strict mode disallows using with statements.

In short, strict mode helps you write more secure and reliable JavaScript code. It’s a good practice to use strict mode in your scripts and functions.

You May Also Like

More From Author

+ There are no comments

Add yours

Leave a Reply