What if the simple word <code>this</code> could secretly cause your code to break in mysterious ways?
Why this in global scope in Javascript? - Purpose & Use Cases
Imagine you want to access a global value or function inside your JavaScript code, but you try to do it by guessing where this points to. You write code assuming this always means the global object, but sometimes it doesn't, causing confusion and bugs.
Manually tracking what this refers to in different parts of your code is slow and error-prone. In the global scope, this can behave differently depending on strict mode or environment, leading to unexpected results and wasted debugging time.
Understanding how this works in the global scope helps you predict its value reliably. You learn that in the global scope, this usually points to the global object (like window in browsers), unless strict mode changes that. This clarity prevents bugs and makes your code easier to read and maintain.
console.log(this); // Unclear if this is global object or undefined in strict mode
console.log(this === window);
// true in browser global scope, clear meaningKnowing this in the global scope lets you write predictable, bug-free code that interacts correctly with global variables and functions.
When you write a script that runs in a browser and want to access or set global variables, understanding this in the global scope helps you avoid mistakes like accidentally creating local variables or getting undefined instead of the global object.
this in global scope usually points to the global object like window.
Strict mode can change this to undefined, causing confusion.
Knowing this behavior prevents bugs and makes your code clearer.
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
