"use strict" in JavaScript: Does It Save You from Errors? Full Breakdown

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

What is "use strict" in JavaScript and Why Is It Controversial?

\n\n

Have you ever written JavaScript code that worked "strangely"? A variable declared itself, this pointed to the global object instead of what you expected, and an error was silently swallowed? Welcome to the world of "dirty" JS.

\n\n

The "use strict" directive is not a function or an operator. It is literally a mode switch for your script or function. When you write "use strict" at the beginning of a file or function, you tell the JavaScript engine: "Hey, don't forgive my mistakes. Be stricter, but more honest."

\n\n

This topic is one of the most popular questions on Stack Overflow (thousands of views). Let's figure out exactly what strict mode does, why it was invented, and how to use it without shooting yourself in the foot.

\n\n

How to Enable Strict Mode?

\n\n

Only two ways:

\n\n
    \n
  • Globally (for the entire script): "use strict"; on the first line of the file.
  • \n
  • Locally (for a function): "use strict"; on the first line of the function body.
  • \n
\n\n
// Global strict mode\n"use strict";\nlet x = 10;\n\n// Local strict mode\nfunction myFunc() {\n  "use strict";\n  let y = 20;\n}
\n\n

Important: If you mix strict and non-strict code in the same file (e.g., by concatenating scripts), problems may arise. It's better to use strict mode for individual functions or modules (ES6 modules are automatically strict).

\n\n

Top 5 Errors Prevented by "use strict"

\n\n

The main goal of "use strict" is to turn JavaScript's "silent" errors into explicit exceptions (errors that are thrown and can be caught). Here are the 5 most common cases.

\n\n

1. Prohibition of Implicit Global Variables

\n\n

This is a classic. In normal mode, if you forget to write var, let, or const, the variable automatically becomes global. This can lead to hard-to-find bugs.

\n\n
// Without "use strict"\nfunction sum(a, b) {\n  total = a + b; // Error? No. total became a global variable!\n}\nsum(2, 3);\nconsole.log(total); // 5 — works, but it's terrible.\n\n// With "use strict"\n"use strict";\nfunction sumStrict(a, b) {\n  total = a + b; // ReferenceError: total is not defined\n}\nsumStrict(2, 3); // Error! The code will crash immediately.
\n\n

How to avoid? Always declare variables. Use let and const instead of var to avoid hoisting and accidental global variables.

\n\n

2. Assigning Values to Non-Writable Properties

\n\n

In normal mode, JavaScript silently ignores attempts to modify NaN, undefined, or a frozen object. In strict mode, an error is thrown.

\n\n
"use strict";\n\n// Attempt to overwrite NaN\nNaN = 123; // TypeError: Cannot assign to read only property 'NaN'\n\n// Attempt to modify a frozen object\nconst obj = Object.freeze({name: "Alice"});\nobj.name = "Bob"; // TypeError: Cannot assign to read only property 'name'
\n\n

How to avoid? Don't try to override built-in constants. Use Object.freeze() deliberately.

\n\n

3. Deleting Variables, Functions, and Arguments

\n\n

You cannot delete a variable declared with var or let, or delete a function. In normal mode, this is simply ignored.

\n\n
"use strict";\n\nvar x = 10;\ndelete x; // SyntaxError: Delete of an unqualified identifier in strict mode.\n\nfunction foo() {}\ndelete foo; // SyntaxError
\n\n

How to avoid? Don't use delete for variables. It is only needed for deleting object properties: delete obj.prop.

\n\n

4. Duplicating Function Parameters

Blogs

Book Recommendations