What does “use strict” do in JavaScript?

Sharing is Caring

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:

Sharing is Caring

Brian is a software architect and technology leader living in Niagara Falls with 13+ years of development experience. He is passionate about automation, business process re-engineering, and building a better tomorrow.

Brian is a proud father of four: two boys, and two girls and has been happily married to Crystal for more than ten years. From time to time, Brian may post about his faith, his family, and definitely about technology.