console.log(this) in global scope returns undefined
console.log(this) in global scope returns undefined
I have created a script which logs the this variable to the console in the global scope. If I launch the script in the browser it returns undefined. eg.
console.log(this); //returns undefined
However if I enter the same command directly in the browser console it returns the window object. Which is what I was expecting. eg.
console.log(this); //returns Window {postMessage: ƒ, blur: ƒ, focus: ƒ, close: ƒ, frames: Window, …}
console.log(this); //returns Window {postMessage: ƒ, blur: ƒ, focus: ƒ, close: ƒ, frames: Window, …}
Why is the behavior/output of logging 'this' to the console different in the browser console and different while expecting the output from a script?
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.