I've recently had a strange issue, where console.log
doesn't output anything if used inside my code but work fine using directly in the console or using window.console.log
.
window.console.log(console);
window.console.log(window.console);
console.log("test1"); // (won't work)
window.console.log("test2");
Ouput:
If i define console myself the console before the code:
var console = window.console;
window.console.log(console);
window.console.log(window.console);
console.log("test1");
window.console.log("test2");
Ouput:
And the strangest thing, if i define console myself after the code
window.console.log(console);
window.console.log(window.console);
console.log("test1");
window.console.log("test2");
var console = window.console;
Ouput:
Note before answering:
It's not a browser-specific issue because the output is the same on every browser (i've checked on chrome/firefox/safari/opera).
It's not being overridden because a log of
console
&window.console
return the same exact object (see screenshot).
This is one of the amazing features of the JavaScript. The
var
directive is processed on the pre-execution stage. So this code:is equivalent to: