Challenge - 5 Problems
Master of this in Global Scope
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
What is the output of this code in a browser?
Consider the following JavaScript code run in a browser environment. What will be logged to the console?
Javascript
console.log(this === window);
Attempts:
2 left
💡 Hint
In browsers, the global object is 'window'.
✗ Incorrect
In the global scope of a browser, 'this' refers to the global object, which is 'window'. So 'this === window' is true.
❓ Predict Output
intermediate2:00remaining
What does this log in Node.js global scope?
In Node.js, what will this code output when run as a script?
Javascript
console.log(this === global);
Attempts:
2 left
💡 Hint
Node.js has a different global object than browsers.
✗ Incorrect
In Node.js, the top-level 'this' in a module is not the global object. It is an empty object, so 'this === global' is false.
❓ Predict Output
advanced2:00remaining
What is the output of this strict mode code?
What will this code output when run in a browser console?
Javascript
"use strict"; console.log(this);
Attempts:
2 left
💡 Hint
Strict mode changes the value of 'this' in the global scope.
✗ Incorrect
In strict mode, 'this' in the global scope is undefined instead of the global object.
❓ Predict Output
advanced2:00remaining
What does this output inside a function in global scope?
What will this code output when run in a browser?
Javascript
function showThis() { console.log(this); } showThis();
Attempts:
2 left
💡 Hint
In non-strict mode, 'this' inside a function called in global scope is the global object.
✗ Incorrect
When a function is called normally in the global scope (non-strict mode), 'this' inside it refers to the global object (window in browsers).
❓ Predict Output
expert2:00remaining
What is the output of this arrow function in global scope?
What will this code output when run in a browser console?
Javascript
const arrowFunc = () => this; console.log(arrowFunc() === window);
Attempts:
2 left
💡 Hint
Arrow functions do not have their own 'this'.
✗ Incorrect
Arrow functions inherit 'this' from their surrounding scope. In global scope, 'this' is the global object (window).