Just went through an interview. The first question asked me was what is console.log()
. I answered with so confidence. Again,
The second question was, what is the difference between window.console.log()
and console.log()
. I was speechless. Tried searching in Google and Stack Overflow. I didn't find such helpful post to understand the difference between them.
Any thoughts is greatly appreciated.
console. The Window. console property returns a reference to the console object, which provides methods for logging information to the browser's console. These methods are intended for debugging purposes only and should not be relied on for presenting information to end users.
Console Object: The Console object provides access to the browser's debugging console, which can be seen using F12 or ctrl+shift+j. Console object contains many methods, log() and dir() are most used among. The console. log() method prints out a toString representation of the object in the console to the user.
alert() stops all interaction with the browser until the message is dismissed while console. log() just prints the message to the console.
print will just print the text to console. console. log() actually records it and we can use it for many purposes like email it for bug report.
In a normal browser context, there is no difference. console
is a global variable, and all globals are properties of the window
object.
console.log(console.log==window.console.log) // true
There are some caveats, such as when not running in the browser, or if the console variable has been reassigned. T.J. Crowder explains it nicely.
If you mean in the default browser JavaScript environment, there is effectively none provided window
and console
haven't been shadowed or reassigned.
In the default browser JavaScript environment, window
is a global that refers to the global object, which is also the window object. The global object holds most globals as properties (it used to be all, but in ES2015 that changed; globals created by let
, const
, or class
are not properties of the global object). But that's not true in most non-browser environments (NodeJS, for instance, uses global
instead of window
), or even in some non-default browser environments (such as a web worker's environment, which doesn't have window
as they can't access the window). So in environments where window
isn't defined, window.console.log
will fail where console.log
wouldn't (provided the environment provides a global console
).
To understand the difference, let's work each of them through:
console.log(...)
means:
console
starting with the current execution context, then the next one, then the next, until it finds it at global scope.log
property on the resulting object.window.console.log(...)
means:
window
starting with the current execution context, then the next one, then the next, until it finds it at global scope.console
property on the resulting object.log
property on the resulting object.So for instance, here's an example where console
has been shadowed, and so console.log
fails whereas window.console.log
works:
function foo() { var console = 42; try { console.log("You WON'T see this."); } catch (e) { } try { window.console.log("You WILL see this."); } catch (e) { } } foo();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With