Suggest an editImprove this articleRefine the answer for “The use strict directive”. Your changes go to moderation before they’re published.Approval requiredContentWhat you’re changing🇺🇸EN🇺🇦UAPreviewTitle (EN)Short answer (EN)**`"use strict"` turns on JavaScript's strict mode: `this` inside a plain function becomes `undefined` instead of the global object, and assigning to an undeclared variable throws a `ReferenceError` instead of silently creating a global one.** The directive applies to the file or the function at whose top it is placed. Strict mode also forbids duplicate parameter names, `delete` of a variable or function, assignment to `undefined`, `NaN` and `Infinity`, the `with` statement, and it gives `eval` its own scope. ```javascript 'use strict'; function showThis() { console.log(this); // undefined, not the global object } showThis(); function test() { x = 10; // ReferenceError: x is not defined } test(); ``` **Key point:** strict mode turns silent failures into explicit errors and removes the implicit binding of `this` to the global object; ES modules and classes are in strict mode automatically.Shown above the full answer for quick recall.Answer (EN)Image**`"use strict"` is a directive that turns on JavaScript's strict mode: `this` inside a plain function becomes `undefined` instead of the global object, and assigning to an undeclared variable becomes an error instead of implicitly creating a global variable.** Strict mode helps you write safer, more predictable code because it removes silent failures and old non-obvious behaviour. ## Theory ### TL;DR - The directive applies to the file or the function at whose top it is written. - `this` in a plain function call becomes `undefined` instead of the global object; methods and arrow functions are unaffected. - Assigning to an undeclared variable throws a `ReferenceError` instead of creating a global variable. - Forbidden: duplicate parameter names, `delete` of a variable or function, assignment to `undefined`, `NaN`, `Infinity`, and the `with` statement. - `eval` gets its own scope and no longer pollutes the surrounding one. ### Quick example ```javascript 'use strict'; function showThis() { console.log(this); } showThis(); // undefined function test() { x = 10; // ReferenceError: x is not defined } test(); ``` The directive goes on the first line of a file: ```javascript 'use strict'; ``` or on the first line of a function: ```javascript function foo() { 'use strict'; // strict mode applies only inside this function } ``` ES modules and class bodies are always in strict mode, so the directive is not needed there. ### The main change: how `this` behaves | Context | Without `"use strict"` | With `"use strict"` | | --- | --- | --- | | In a plain function | `this` -> global object (`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 the outside | inherited from the outside, unchanged | Without strict mode: ```javascript function showThis() { console.log(this); } showThis(); // the global object (window in a browser) ``` Here the engine substitutes the global object by default. 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" binding. That protects you from accidentally touching the global object (`window` in a browser, `global` in Node.js) and from silently creating global properties. ### No more implicit globals Without strict mode: ```javascript function test() { x = 10; // the variable is created globally! console.log(x); } test(); console.log(globalThis.x); // 10 ``` With strict mode: ```javascript 'use strict'; function test() { x = 10; // ReferenceError: x is not defined } test(); ``` Variables now have to be declared explicitly with `let`, `const` or `var`, otherwise you get an error. This removes a whole class of bugs where a single typo in a variable name silently creates a new global. ### Syntax bans: duplicates, delete and with Duplicate parameter names. Without strict mode: ```javascript function sum(a, a, c) { return a + a + c; } console.log(sum(1, 2, 3)); // 7 ``` With strict mode: ```javascript 'use strict'; function sum(a, a, c) { // SyntaxError return a + a + c; } ``` Deleting variables, functions and parameters. Without strict mode: ```javascript var a = 1; delete a; // false ``` With strict mode: ```javascript 'use strict'; var a = 1; delete a; // SyntaxError ``` In strict mode you cannot delete anything that is not a property of an object. The `with` statement is banned outright as well, because it makes scope resolution unpredictable. ### Silent failures become loud Assignment to `undefined`, `NaN` and `Infinity`: ```javascript 'use strict'; undefined = 5; // TypeError NaN = 123; // TypeError Infinity = 42; // TypeError ``` Side by side: | Situation | Without `"use strict"` | With `"use strict"` | | --- | --- | --- | | Assigning to `NaN`, `undefined`, `Infinity` | silently ignored | error | | Assigning to a `const` | ignored | error | | The `with` statement | allowed | forbidden | | Deleting non-deletable properties | silently ignored | error | That is the whole point of strict mode: an operation that used to do nothing and say nothing now fails immediately, so the bug shows up during development rather than in production. ### Safer eval, and the summary table Without strict mode `eval` can create variables in the current context: ```javascript eval('var x = 10;'); console.log(x); // 10 ``` With strict mode `eval` gets its own scope: ```javascript 'use strict'; eval('var x = 10;'); console.log(x); // ReferenceError ``` This prevents accidental pollution of the surrounding environment. | What `"use strict"` changes | Behaviour | | --- | --- | | `this` in plain functions | `undefined` instead of the global object | | Undeclared variables | Error | | Duplicate parameters | Error | | Deleting variables and functions | Error | | Assigning to `NaN`, `undefined`, `Infinity` | Error | | `eval` | Gets its own scope | | `with` | Forbidden | ### Common mistakes - Putting the directive somewhere other than the first line of the file or function: then it is just a string literal and strict mode never turns on. - Assuming `"use strict"` applies to the whole project. It applies only within its own file or its own function. - Adding the directive to an ES module or a class: strict mode is already on there by specification. - Forgetting that in strict mode `this` inside a plain function is `undefined` and reaching for `this.something` after losing the context: instead of a silent global object you get a `TypeError`. - Concatenating files where one of them starts with `"use strict"`: the directive at the top of the result can unexpectedly switch the whole bundle into strict mode.For the reviewerNote to the moderator (optional)Visible only to the moderator. Helps review go faster.