Suggest an editImprove this articleRefine the answer for “use strict”. Your changes go to moderation before they’re published.Approval requiredContentWhat you’re changing🇺🇸EN🇺🇦UAPreviewTitle (EN)Short answer (EN)**`"use strict"`** is a directive that turns on **strict mode interpretation of JavaScript**, helping you write safer, more predictable code by eliminating "silent" errors and old, non-obvious behavior. **Key point:** in strict mode, `this` in regular functions becomes `undefined` instead of `window`, and undeclared variables, duplicate parameters, and other unsafe actions throw an error.Shown above the full answer for quick recall.Answer (EN)Image## 1. What `"use strict"` is > `"use strict"` is a directive that turns on **strict mode interpretation of JavaScript**. > > It helps you write **safer, more predictable code** by eliminating "silent" errors and old, non-obvious behavior. Strict mode applies **within the file or function** where it is declared at the top: ```javascript "use strict"; ``` or: ```javascript function foo() { "use strict"; // strict mode only inside this function } ``` --- ## 2. The main change - the behavior of `this` | Context | Without `"use strict"` | With `"use strict"` | |---|---|---| | In a regular function | `this` → `window` (in a browser) | `this` → `undefined` | | In an object method | `this` → the object itself | `this` → the same object (unchanged) | | In an arrow function | inherited from outside | inherited from outside (unchanged) | --- ### Example without strict mode ```javascript function showThis() { console.log(this); } showThis(); // window (in a browser) ``` Here JS "substitutes" the global object (`window`) by default. --- ### Example with strict mode ```javascript "use strict"; function showThis() { console.log(this); } showThis(); // undefined ``` In strict mode, if a function is called **not as a method of an object**, `this` stays `undefined` - no implicit "magic" bindings. This helps avoid accidental references to the global object (such as `window` or `global`). --- ## 3. Strict mode forbids implicit variable creation Without strict mode: ```javascript function test() { x = 10; // the variable gets created globally! console.log(x); } test(); console.log(window.x); // 10 ``` With `"use strict"`: ```javascript "use strict"; function test() { x = 10; // ReferenceError: x is not defined } test(); ``` Now you must **declare variables explicitly** (`let`, `const`, `var`), otherwise you get an error. --- ## 4. Forbids duplicate variable and parameter names Without strict mode: ```javascript function sum(a, a, c) { return a + a + c; } console.log(sum(1, 2, 3)); // 7 ``` With `"use strict"`: ```javascript "use strict"; function sum(a, a, c) { // SyntaxError return a + a + c; } ``` This prevents mistakes and makes the code more predictable. --- ## 5. Forbids deleting variables, functions, and parameters Without strict mode: ```javascript var a = 1; delete a; // false ``` With `"use strict"`: ```javascript "use strict"; var a = 1; delete a; // SyntaxError ``` In strict mode, you cannot delete anything that is not a property of an object. --- ## 6. Forbids assigning a value to `NaN`, `undefined`, `Infinity` ```javascript "use strict"; undefined = 5; // TypeError NaN = 123; // TypeError Infinity = 42; // TypeError ``` --- ## 7. Improves the safety of `eval` Without strict mode, `eval` can create variables in the current context: ```javascript eval("var x = 10;"); console.log(x); // 10 ``` With strict mode, `eval` creates its own scope: ```javascript "use strict"; eval("var x = 10;"); console.log(x); // ReferenceError ``` This prevents accidental pollution of the environment. --- ## 8. Protects against implicit "soft" errors | Situation | Without `"use strict"` | With `"use strict"` | |---|---|---| | Assigning to `NaN`, `undefined`, `Infinity` | silently ignored | error | | Assigning to `const` | ignored | error | | The `with` statement | allowed | not allowed | | Deleting properties from objects | silently ignored | error | --- ## SUMMARY | What `"use strict"` changes | Behavior | |---|---| | `this` in regular functions | `undefined` instead of `window` | | Undeclared variables | Error | | Duplicate parameters | Error | | Deleting variables/functions | Error | | Assigning to `NaN`, `undefined`, `Infinity` | Error | | `eval` | Creates its own scope | | `with` | Forbidden |For the reviewerNote to the moderator (optional)Visible only to the moderator. Helps review go faster.