What is "use strict" in JavaScript and Why Is It Controversial?
\n\nHave 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.
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."
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\nHow to Enable Strict Mode?
\n\nOnly 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
// 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\nImportant: 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\nTop 5 Errors Prevented by "use strict"
\n\nThe 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.
1. Prohibition of Implicit Global Variables
\n\nThis 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.
// 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\nHow to avoid? Always declare variables. Use let and const instead of var to avoid hoisting and accidental global variables.
2. Assigning Values to Non-Writable Properties
\n\nIn normal mode, JavaScript silently ignores attempts to modify NaN, undefined, or a frozen object. In strict mode, an error is thrown.
"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\nHow to avoid? Don't try to override built-in constants. Use Object.freeze() deliberately.
3. Deleting Variables, Functions, and Arguments
\n\nYou cannot delete a variable declared with var or let, or delete a function. In normal mode, this is simply ignored.
"use strict";\n\nvar x = 10;\ndelete x; // SyntaxError: Delete of an unqualified identifier in strict mode.\n\nfunction foo() {}\ndelete foo; // SyntaxError\n\nHow to avoid? Don't use delete for variables. It is only needed for deleting object properties: delete obj.prop.