Skip to main content

use strict

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

ContextWithout "use strict"With "use strict"
In a regular functionthiswindow (in a browser)thisundefined
In an object methodthis → the object itselfthis → the same object (unchanged)
In an arrow functioninherited from outsideinherited 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

SituationWithout "use strict"With "use strict"
Assigning to NaN, undefined, Infinitysilently ignorederror
Assigning to constignorederror
The with statementallowednot allowed
Deleting properties from objectssilently ignorederror

SUMMARY

What "use strict" changesBehavior
this in regular functionsundefined instead of window
Undeclared variablesError
Duplicate parametersError
Deleting variables/functionsError
Assigning to NaN, undefined, InfinityError
evalCreates its own scope
withForbidden

Short Answer

Interview ready
Premium

A concise answer to help you respond confidently on this topic during an interview.