What is the difference between window.console.log and console.log

Santosh picture Santosh · Jan 3, 2017 · Viewed 12.5k times · Source

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.

Answer

Scimonster picture Scimonster · Jan 3, 2017

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.