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.
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.