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.
thisin a plain function call becomesundefinedinstead of the global object; methods and arrow functions are unaffected.- Assigning to an undeclared variable throws a
ReferenceErrorinstead of creating a global variable. - Forbidden: duplicate parameter names,
deleteof a variable or function, assignment toundefined,NaN,Infinity, and thewithstatement. evalgets its own scope and no longer pollutes the surrounding one.
Quick example
'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:
'use strict';or on the first line of a function:
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:
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:
'use strict';
function showThis() {
console.log(this);
}
showThis(); // undefinedIn 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:
function test() {
x = 10; // the variable is created globally!
console.log(x);
}
test();
console.log(globalThis.x); // 10With strict mode:
'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:
function sum(a, a, c) {
return a + a + c;
}
console.log(sum(1, 2, 3)); // 7With strict mode:
'use strict';
function sum(a, a, c) { // SyntaxError
return a + a + c;
}Deleting variables, functions and parameters. Without strict mode:
var a = 1;
delete a; // falseWith strict mode:
'use strict';
var a = 1;
delete a; // SyntaxErrorIn 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:
'use strict';
undefined = 5; // TypeError
NaN = 123; // TypeError
Infinity = 42; // TypeErrorSide 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:
eval('var x = 10;');
console.log(x); // 10With strict mode eval gets its own scope:
'use strict';
eval('var x = 10;');
console.log(x); // ReferenceErrorThis 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
thisinside a plain function isundefinedand reaching forthis.somethingafter losing the context: instead of a silent global object you get aTypeError. - 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 readyA concise answer to help you respond confidently on this topic during an interview.