0
0
Javascriptprogramming~5 mins

this in global scope in Javascript - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
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
What is the value of this in the global scope when running JavaScript in strict mode?
Aundefined
Bwindow object
Cglobal object
Dnull
In Node.js modules, what does this refer to at the top-level global scope?
Aan empty object
Bmodule.exports
Cglobal object
Dundefined
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
What will console.log(this === window); output in a browser's global scope (non-strict mode)?
Afalse
Btrue
Cundefined
DTypeError
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.