0
0
Javascriptprogramming~10 mins

this in global scope in Javascript - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to log the global this value in a browser environment.

Javascript
console.log([1]);
Drag options to blanks, or click blank then click option'
Aself
Bthis
Cwindow
DglobalThis
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'this' directly in strict mode returns undefined.
Using 'window' only works in browsers, not in Node.js.
2fill in blank
medium

Complete the code to check if 'this' in the global scope equals the global object.

Javascript
console.log(this === [1]);
Drag options to blanks, or click blank then click option'
Aself
Bwindow
CglobalThis
Dglobal
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'global' which is Node.js specific.
Using 'window' which is browser specific.
3fill in blank
hard

Fix the error in the code to correctly log the global object using 'this' in a function.

Javascript
function showGlobal() {
  'use strict';
  console.log([1]);
}
showGlobal();
Drag options to blanks, or click blank then click option'
AglobalThis
Bthis
Cwindow
Dself
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'this' inside strict mode function returns undefined.
Using 'window' which may not exist in all environments.
4fill in blank
hard

Fill both blanks to create a function that returns true if 'this' in global scope equals the global object.

Javascript
function checkGlobal() {
  return this === [1] || this === [2];
}
console.log(checkGlobal());
Drag options to blanks, or click blank then click option'
AglobalThis
Bwindow
Cself
Dglobal
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'global' which is Node.js specific.
Using 'self' which is less common.
5fill in blank
hard

Fill all three blanks to create an arrow function that returns the global object using 'this' and fallback options.

Javascript
const getGlobal = () => [1] || [2] || [3];
console.log(getGlobal());
Drag options to blanks, or click blank then click option'
AglobalThis
Bwindow
Cself
Dthis
Attempts:
3 left
💡 Hint
Common Mistakes
Using only 'this' which may be undefined in arrow functions.
Not providing fallback options.