Member-only story

Why Should I Put “use strict” in My JavaScript Files?

Rich Brown
3 min readMay 16, 2021

--

Photo by Author, 2021

“use strict”;

Adding the key words “use strict” at the top of your file or functions places it in a strict operating context. The main benefit is it makes debugging easier.

Many errors that would be ignored or fail silently now generate errors or throw exceptions. It alerts you to problems with your code and directs you quickly to the probable source.

The most common way of turning this on is to place the phrase “use strict”; in quotes at the top of the file — before any other code. Why are these words in quotes instead of using key words? Remember all approved changes to JavaScript must be backward compatible and when this feature was first introduced, many of the browsers at the time did not support it. As it is a string the older browses ignored it while the new ones supported it.

What kind of problems are found? For instance:

today = "Monday";

creates the variable today in the global scope. In the browser the global scope is window. In node.js the global scope is global. In this context, console.log(today) would print Monday to the console.

Let’s see how this could be a problem. First we create a variable today properly:

var myValue = 1;

--

--

Rich Brown
Rich Brown

Written by Rich Brown

Passionate about using AI to enhance daily living, boost productivity, and unleash creativity. Contact: richbrowndigital@gmail.com

No responses yet