Skip to main content

The use strict directive

"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

ContextWithout "use strict"With "use strict"
In a plain functionthis -> global object (window in a browser)this -> undefined
In an object methodthis -> the object itselfthis -> the same object, unchanged
In an arrow functioninherited from the outsideinherited 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:

SituationWithout "use strict"With "use strict"
Assigning to NaN, undefined, Infinitysilently ignorederror
Assigning to a constignorederror
The with statementallowedforbidden
Deleting non-deletable propertiessilently ignorederror

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" changesBehaviour
this in plain functionsundefined instead of the global object
Undeclared variablesError
Duplicate parametersError
Deleting variables and functionsError
Assigning to NaN, undefined, InfinityError
evalGets its own scope
withForbidden

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.

Short Answer

Interview ready
Premium

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