The this keyword helps you access the current context. In the global scope, it points to the main global object, letting you reach global variables or functions easily.
this in global scope in Javascript
console.log(this);
In browsers, this in global scope points to the window object.
In Node.js, this in the top-level global scope is an empty object, not the global object.
console.log(this);
var become properties of the global object.var a = 10; console.log(this.a);
this in global scope is undefined."use strict"; console.log(this);
This program shows what this points to in global scope and inside a function in strict mode.
"use strict"; var name = "Global"; function showThis() { console.log(this); } console.log(this); // global scope showThis(); // inside function
In browsers, this in global scope usually equals window.
Using "use strict"; changes this to undefined in global scope and functions.
Global variables declared with var become properties of the global object, but let and const do not.
this in global scope points to the global object (like window in browsers).
Strict mode changes this to undefined in global scope.
Understanding this helps you know where your code runs and what it can access.
Practice
this refer to when used in the global scope (outside any function) in a browser environment?Solution
Step 1: Understand global scope context
In the global scope,thisrefers to the global object in JavaScript.Step 2: Identify the global object in browsers
In browsers, the global object iswindow, sothispoints towindow.Final Answer:
The global object, which iswindowin browsers -> Option CQuick Check:
thisin global scope =window[OK]
this is window in browsers [OK]- Thinking
thisis undefined in global scope by default - Confusing
thiswith the current function - Assuming
thispoints todocument
this in the global scope refers to the window object?Solution
Step 1: Recall global
In the browser global scope,thisin browsersthispoints to thewindowobject.Step 2: Compare
Usingthiswithwindowthis === windowreturnstrueifthisis the global object.Final Answer:
console.log(this === window); -> Option BQuick Check:
this === windowis true in global scope [OK]
this === window to confirm global this [OK]- Using
documentinstead ofwindow - Using
globalwhich is Node.js specific - Comparing
thistonull
console.log(this === window); 'use strict'; console.log(this);
Solution
Step 1: Evaluate first console.log before strict mode
Before'use strict',thisin global scope equalswindow, sothis === windowistrue.Step 2: Evaluate second console.log after strict mode
Strict mode affectsthisinside functions, but in global scopethisstill points towindow, so it logs thewindowobject.Final Answer:
true and thenwindowobject -> Option DQuick Check:
Globalthis=windoweven with strict mode [OK]
this in browsers [OK]- Assuming
thisbecomes undefined in global scope with strict mode - Confusing strict mode effects inside functions vs global scope
- Thinking
thisequalsglobalin browsers
'use strict';
function checkThis() {
console.log(this);
}
checkThis();What is the problem if you want to log the global object using
this inside checkThis?Solution
Step 1: Understand strict mode effect inside functions
In strict mode,thisinside a regular function called without an object isundefined.Step 2: Check the function call context
The functioncheckThisis called directly, sothisis not bound to the global object.Final Answer:
thisisundefinedbecause of strict mode inside the function -> Option AQuick Check:
Strict mode makesthisundefined in functions called globally [OK]
this to undefined inside functions [OK]- Assuming
thisalways points to global object - Thinking strict mode causes syntax errors here
- Confusing function
thiswith globalthis
myGlobal and access it via this.myGlobal in the global scope under strict mode. Which code snippet correctly achieves this in a browser environment?Solution
Step 1: Understand variable declaration and global object
In strict mode, variables declared withvar,let, orconstdo not become properties ofwindow.Step 2: Check how to explicitly add property to global object
Assigningwindow.myGlobal = 10explicitly addsmyGlobalas a property of the global object.Final Answer:
window.myGlobal = 10; console.log(this.myGlobal); -> Option AQuick Check:
Onlywindow.myGlobalensuresthis.myGlobalexists globally [OK]
window.myGlobal to add global property reliably [OK]- Using
letorconstexpecting globalthisproperty - Assuming
varalways adds property tothisin all contexts - Not understanding difference between variable and global object property
Ready to prove it?
Pass the test and this pattern moves unknown
