Complete the code to log the global this value in a browser environment.
console.log([1]);In modern JavaScript, globalThis is the standard way to access the global object in any environment.
Complete the code to check if 'this' in the global scope equals the global object.
console.log(this === [1]);this in the global scope equals globalThis in non-strict mode.
Fix the error in the code to correctly log the global object using 'this' in a function.
function showGlobal() {
'use strict';
console.log([1]);
}
showGlobal();In strict mode, this inside a function is undefined, so use globalThis to access the global object.
Fill both blanks to create a function that returns true if 'this' in global scope equals the global object.
function checkGlobal() {
return this === [1] || this === [2];
}
console.log(checkGlobal());This function checks if this is either globalThis or window, covering most browser environments.
Fill all three blanks to create an arrow function that returns the global object using 'this' and fallback options.
const getGlobal = () => [1] || [2] || [3]; console.log(getGlobal());
Arrow functions don't have their own this, so fallback to window or self to get the global object in browsers.