Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 914
Santosh Avatar asked Jan 03 '17 18:01

Santosh


People also ask

What is window console log?

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.

What is the difference between console and log?

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.

What is the difference between console log and alert?

alert() stops all interaction with the browser until the message is dismissed while console. log() just prints the message to the console.

What is the difference between console log and print?

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.


2 Answers

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.

like image 111
Scimonster Avatar answered Oct 16 '22 10:10

Scimonster


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:

  1. The JavaScript engine has to search for the a binding for the identifier console starting with the current execution context, then the next one, then the next, until it finds it at global scope.
  2. Then it looks up the log property on the resulting object.
  3. Then it calls it

window.console.log(...) means:

  1. The JavaScript engine has to search for the a binding for the identifier window starting with the current execution context, then the next one, then the next, until it finds it at global scope.
  2. Then it looks up the console property on the resulting object.
  3. Then it looks up the log property on the resulting object.
  4. Then it calls it

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();
like image 30
T.J. Crowder Avatar answered Oct 16 '22 09:10

T.J. Crowder