0
0
Javascriptprogramming~15 mins

this in global scope in Javascript - Mini Project: Build & Apply

Choose your learning style9 modes available
Understanding <code>this</code> in Global Scope
📖 Scenario: Imagine you are learning how JavaScript handles the keyword this when used outside of any function or object. This is important because this behaves differently depending on where you use it.
🎯 Goal: You will write code to see what this refers to in the global scope and print its value.
📋 What You'll Learn
Create a variable called globalThisValue and assign it the value of this in the global scope.
Create a variable called isGlobalWindow that checks if globalThisValue is the same as the global window object.
Print the value of globalThisValue.
Print the value of isGlobalWindow.
💡 Why This Matters
🌍 Real World
Understanding <code>this</code> in the global scope helps you know what object your code is referring to when you use <code>this</code> outside of functions or objects.
💼 Career
Many JavaScript jobs require understanding how <code>this</code> works to avoid bugs and write clear code, especially when working with browser APIs or frameworks.
Progress0 / 4 steps
1
Create a variable globalThisValue and assign it the value of this in the global scope
Write a line of code to create a variable called globalThisValue and set it equal to this in the global scope.
Javascript
Need a hint?

In JavaScript, this in the global scope refers to the global object. Just write const globalThisValue = this;

2
Create a variable isGlobalWindow to check if globalThisValue is the same as window
Write a line of code to create a variable called isGlobalWindow and set it to the result of comparing globalThisValue with window using the strict equality operator ===.
Javascript
Need a hint?

Use === to check if globalThisValue and window are exactly the same object.

3
Print the value of globalThisValue
Write a line of code to print the value of globalThisValue using console.log.
Javascript
Need a hint?

Use console.log(globalThisValue); to print the value.

4
Print the value of isGlobalWindow
Write a line of code to print the value of isGlobalWindow using console.log.
Javascript
Need a hint?

Use console.log(isGlobalWindow); to print the boolean result.