Ask any question about JavaScript here... and get an instant response.
Post this Question & Answer:
Why does 'this' inside a function behave differently in strict mode?
Asked on Jan 22, 2026
Answer
In JavaScript, "this" inside a function behaves differently in strict mode because strict mode enforces a more predictable and safer set of rules. In non-strict mode, "this" defaults to the global object, but in strict mode, "this" becomes "undefined" if not explicitly set.
<!-- BEGIN COPY / PASTE -->
function nonStrictFunction() {
console.log(this); // Logs the global object (window in browsers)
}
nonStrictFunction();
function strictFunction() {
'use strict';
console.log(this); // Logs undefined
}
strictFunction();
<!-- END COPY / PASTE -->Additional Comment:
✅ Answered with JavaScript best practices.- In non-strict mode, calling a function without a specific context sets "this" to the global object.
- In strict mode, "this" is "undefined" when a function is called without a specific context.
- Using strict mode helps prevent accidental modifications to the global object and can catch common coding errors.
Recommended Links:
