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:
"use strict";or:
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
function showThis() {
console.log(this);
}
showThis(); // window (in a browser)Here JS "substitutes" the global object (window) by default.
Example 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" 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:
function test() {
x = 10; // the variable gets created globally!
console.log(x);
}
test();
console.log(window.x); // 10With "use strict":
"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:
function sum(a, a, c) {
return a + a + c;
}
console.log(sum(1, 2, 3)); // 7With "use strict":
"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:
var a = 1;
delete a; // falseWith "use strict":
"use strict";
var a = 1;
delete a; // SyntaxErrorIn strict mode, you cannot delete anything that is not a property of an object.
6. Forbids assigning a value to NaN, undefined, Infinity
"use strict";
undefined = 5; // TypeError
NaN = 123; // TypeError
Infinity = 42; // TypeError7. Improves the safety of eval
Without strict mode, eval can create variables in the current context:
eval("var x = 10;");
console.log(x); // 10With strict mode, eval creates its own scope:
"use strict";
eval("var x = 10;");
console.log(x); // ReferenceErrorThis 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 |
Short Answer
Interview readyA concise answer to help you respond confidently on this topic during an interview.