“Strict” Mode and Quality Code

Chris Benedict
Apr 22 · 4 min read

Don’t leave home without it!

Have you ever felt like the code to which you have given precious life lacks discipline and gets away with practically everything? Does its carefree, devil-may-care attitude make your lips curl and your blood boil? Do you constantly suppress a raging inferno of disapproval and indignation as a result of a swelling suspicion that the creation to which you have tirelessly donated your blood, sweat, and tears is silently undermining you at every semicolon? Not to worry — I have the topical solution for you! With one quick and easy application of “use strict” at the crown of your Javascript files, you can become the overbearing, veracious parent you’ve always aspired to be. Kiss those pesky, inaccurate, antiquated features goodbye! Thanks, ECMAscript5!

“But, why bother? It’ll learn how to become a productive, considerate contribution to society someday, right?” WRONG.

I kid, I kid. But, if you understand the value of accurate, readable, and efficient code, then the use of “strict” mode is for you!

Strict Mode: Engaged!

A “strict” operating context is important and desirable for a number of reasons and can be used in the global context for the entire script or within individual functions. First, where your code could fail silently or produce a desired outcome, but with “unsafe” and syntactically inaccurate code, a more precise context will throw errors that allow you to improve your code.

For example, if a variable is declared without the use of var, let, or const within a function and the function is called, the variable then becomes a global variable, accessible outside of this function, and the function executes without throwing an error. This can produce problems down the line. However, by declaring “use strict” inside of our function, our function fails and we can correct the error.

“strict” mode used within a function

In this example, when we call “notStrict” and pass it a parameter, our function executes and returns an output despite the lack of a variable keyword before “y”. Conversely, by adding “use strict” to the top of our function, we prohibit the forgiving nature of Javascript and throw an error.

But, what do I mean by failing silently? In “sloppy” (or “normal”) mode, certain actions may not be permitted, but do not provide us with a reason by sending an error to the console. We may expect we can delete variables or assign values to ‘read-only’ properties without using ‘strict’ mode because, well, nothing happens, but errors are super helpful!

“strict” mode used in the global context

Line 3 doesn’t do much to our variable, but there are no errors thrown!

Similarly, we cannot assign a new value to x in line 8, but the code fails silently.

However, when in a “strict” operating context, we can see exactly what we are doing wrong.

Assignments to non-writable globals like undefined, NaN, Infinity, etc also throw exceptions.

“Strict” mode also prevents the duplication of parameters in a function.

When we call the “notStrictSum” function, it returns a value of 16 even though we use “x” twice in our parameters. “But, Chris — isn’t 4 + 5 + 6 = 15?” Yes, yes it is. In this context, x is defined by our first parameter (x = 4), then reassigned (x = 5), then y is assigned (y = 6). Our function then permits the execution of line 2, which returns 5 + 5 + 6.

However, we can see that our “strictSum” function throws a Syntax Error because it prohibits the use of duplicate parameters.

Additionally, “use strict” will prohibit the use of the ‘eval’ name/keyword and with(){} statements. Sorry, were you using these?


With that, I leave you with a nice little summary of the benefits of using “strict” mode in your Javascript files, provided by the techies over at geeksforgeeks:

Strict mode makes several changes to normal JavaScript semantics.

  • Strict mode eliminates some JavaScript silent errors by changing them to throw errors.
  • Strict mode fixes mistakes that make it difficult for JavaScript engines to perform optimizations: strict mode code can sometimes be made to run faster than identical code that’s not strict mode.
  • Strict mode prohibits some syntax likely to be defined in future versions of ECMAScript.
  • It prevents, or throws 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.
  • Strict mode makes it easier to write “secure” JavaScript.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade