What does this refer to in the global scope in JavaScript?
In the global scope, this refers to the global object. In browsers, this is usually the window object.
Click to reveal answer
intermediate
How does this behave differently in strict mode in the global scope?
In strict mode, this in the global scope is undefined instead of the global object.
Click to reveal answer
intermediate
What is the global object in Node.js when using this in the global scope?
In Node.js, the global object is global, but this in the top-level global scope of a module is not global; it is an empty object {}.
Click to reveal answer
beginner
Code snippet:
console.log(this);
What will this print in a browser's global scope?
It will print the window object, which is the global object in browsers.
Click to reveal answer
beginner
Why is understanding this in the global scope important?
Because it helps you know what object your code is referring to, avoiding bugs and unexpected behavior especially when switching between strict mode and non-strict mode or different environments.
Click to reveal answer
In a browser, what does this refer to in the global scope (non-strict mode)?
Adocument object
Bundefined
Cnull
Dwindow object
✗ Incorrect
this in the global scope refers to the global object, which is window in browsers.
What is the value of this in the global scope when running JavaScript in strict mode?
Aundefined
Bwindow object
Cglobal object
Dnull
✗ Incorrect
In strict mode, this in the global scope is undefined.
In Node.js modules, what does this refer to at the top-level global scope?
Aan empty object
Bmodule.exports
Cglobal object
Dundefined
✗ Incorrect
In Node.js modules, this at the top-level is an empty object, not the global object.
Which of the following is true about this in the global scope?
A<code>this</code> always refers to the global object
B<code>this</code> is always undefined
C<code>this</code> can be undefined in strict mode
D<code>this</code> refers to the current function
✗ Incorrect
In strict mode, this in the global scope is undefined; otherwise, it refers to the global object.
What will console.log(this === window); output in a browser's global scope (non-strict mode)?
Afalse
Btrue
Cundefined
DTypeError
✗ Incorrect
In the browser global scope, this is the window object, so the comparison is true.
Explain what this refers to in the global scope in JavaScript and how strict mode changes it.
Think about what object <code>this</code> points to when you run code outside any function.
You got /4 concepts.
Describe how this behaves differently in the global scope between browser and Node.js environments.
Consider the global objects and module system in both environments.
You got /4 concepts.
Practice
(1/5)
1. In JavaScript, what does this refer to when used in the global scope (outside any function) in a browser environment?
easy
A. The document object
B. The current function
C. The global object, which is window in browsers
D. Undefined
Solution
Step 1: Understand global scope context
In the global scope, this refers to the global object in JavaScript.
Step 2: Identify the global object in browsers
In browsers, the global object is window, so this points to window.
Final Answer:
The global object, which is window in browsers -> Option C
Quick Check:
this in global scope = window [OK]
Hint: Remember: global this is window in browsers [OK]
Common Mistakes:
Thinking this is undefined in global scope by default
Confusing this with the current function
Assuming this points to document
2. Which of the following is the correct way to check if this in the global scope refers to the window object?
easy
A. console.log(this === document);
B. console.log(this === window);
C. console.log(this === global);
D. console.log(this === null);
Solution
Step 1: Recall global this in browsers
In the browser global scope, this points to the window object.
Step 2: Compare this with window
Using this === window returns true if this is the global object.
Final Answer:
console.log(this === window); -> Option B
Quick Check:
this === window is true in global scope [OK]
Hint: Check this === window to confirm global this [OK]
Common Mistakes:
Using document instead of window
Using global which is Node.js specific
Comparing this to null
3. What will be the output of the following code in a browser's global scope?
In strict mode, this inside a regular function called without an object is undefined.
Step 2: Check the function call context
The function checkThis is called directly, so this is not bound to the global object.
Final Answer:
this is undefined because of strict mode inside the function -> Option A
Quick Check:
Strict mode makes this undefined in functions called globally [OK]
Hint: Strict mode sets this to undefined inside functions [OK]
Common Mistakes:
Assuming this always points to global object
Thinking strict mode causes syntax errors here
Confusing function this with global this
5. You want to create a global variable myGlobal and access it via this.myGlobal in the global scope under strict mode. Which code snippet correctly achieves this in a browser environment?
hard
A. window.myGlobal = 10; console.log(this.myGlobal);
B. let myGlobal = 10; console.log(this.myGlobal);
C. const myGlobal = 10; console.log(this.myGlobal);
D. var myGlobal = 10; console.log(this.myGlobal);
Solution
Step 1: Understand variable declaration and global object
In strict mode, variables declared with var, let, or const do not become properties of window.
Step 2: Check how to explicitly add property to global object
Assigning window.myGlobal = 10 explicitly adds myGlobal as a property of the global object.
Final Answer:
window.myGlobal = 10; console.log(this.myGlobal); -> Option A
Quick Check:
Only window.myGlobal ensures this.myGlobal exists globally [OK]
Hint: Use window.myGlobal to add global property reliably [OK]
Common Mistakes:
Using let or const expecting global this property
Assuming var always adds property to this in all contexts
Not understanding difference between variable and global object property
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.