28

Do all browsers support this? I would like to output an error using console.log() but was wondering if this supported by all browsers?

console.log("Error etc");
KingKongFrog
  • 13,946
  • 21
  • 75
  • 124
  • 6
    You can check browser compatibility at [mdn](https://developer.mozilla.org/en-US/docs/DOM/console.log) – jeremy Dec 29 '12 at 23:01
  • 1
    In general: don't count on it. It's never a critical feature, and writing the fallback is pretty straightforward, so whatever. – Matchu Dec 29 '12 at 23:03
  • 7
    `if(typeof window.console == 'undefined') { window.console = {log: function (msg) {} }; }` – Matchu Dec 29 '12 at 23:05
  • 3
    I had the same issue as this comment, http://stackoverflow.com/questions/5472938/does-ie9-support-console-log-and-is-it-a-real-function#comment15214547_5473193 – bradcush Feb 26 '14 at 17:33
  • 1
    check caniuse.com... – chesscov77 May 26 '17 at 05:18
  • Some tips: You can use colors to have a better view of: console.log('%c Sample Text', 'color:green;'); Or add some VAR in the text using: console.log(\`Sample ${variable}\`, 'color:green;'); – Gilberto B. Terra Jr. Jan 29 '19 at 11:32

8 Answers8

13

No, not all browsers support console.log as it is not part of the standard and is an extension of the DOM and thus you should not count on its presence. To make your code resilient you should assume it does not exist and code accordingly.

Aaron McIver
  • 24,527
  • 5
  • 59
  • 88
  • 10
    Whether something is part of a standard and whether it's supported by all browsers (or whatever technology in question) is hardly the same thing. A more down-to-earth rationale is that yes, there are older version of IE that might trip if you rely on console.log's existence - for the sake of you app rather than the holy spirit of standards compliance. – John Nov 16 '13 at 21:59
  • 2
    @John Almost 3 years later I would state that your app should not have to use this in a produciton deployment. Although all major browsers support console.log in their current iterations does not guarantee people will use major browsers or browsers at all to execute the code. The best reference I can make to this point is at MDN: https://developer.mozilla.org/en-US/docs/Web/API/Console/log - the very first statement on this page is: "This feature is non-standard and is not on a standards track. Do not use it on production sites facing the Web". WebApp or not this is correct practice. – Ryan Rentfro May 21 '16 at 07:35
  • A November 2017 update... I notice that the MDN document linked in an earlier comment no longer refers to console.log() as "non-standard". It refers instead to the following WHATWG Living Standard: https://console.spec.whatwg.org/ – Jonathan Nicol Nov 14 '17 at 10:30
12

I've done something like this in the past:

// Log to native console if possible, alert otherwise
window.console = typeof window.console === 'undefined'
    ? {log:function(/* polymorphic */){alert(arguments)}}
    : window.console;

You can put that at the "top" of your JS and it works pretty nicely when you're forced to do debugging w/ a browser that doesn't support console, and doesn't require you to change your other JS source that's already calling console.log all over the place. Of course you might want to do something other than alert to preserve your sanity...

http://jsfiddle.net/4dty5/

Madbreaks
  • 19,094
  • 7
  • 58
  • 72
9

It is now August 2015, and I think the current answer to this question is:

All major browsers on mobile and desktop support console.log. (caniuse)

It is not part of any standard, but it is now an expected deliverable of a complete modern browser.

However:

If you need to support old browsers (IE<10), more mobile browsers, or users running experimental browsers, then it might be a good idea to use a polyfill (1,2,3,4) to ensure window.console exists.

It might not work in WebWorkers on all browsers. (MDN)

In UC Browser and Opera Mini the functions will run, but you won't see the output. (caniuse)

But for the vast majority of web users now, we can assume console.log will work as expected.

joeytwiddle
  • 29,306
  • 13
  • 121
  • 110
8

Make a wrapper function:

function log(text) {
  if (window.console) {
     window.console.log(text);
  }
}
asgoth
  • 35,552
  • 12
  • 89
  • 98
8

This code will check to see if the function exists and create a dummy in case it does not. Credit: StackOverflow

if (!window.console) window.console = {};
if (!window.console.log) window.console.log = function () { };
Community
  • 1
  • 1
ow3n
  • 5,974
  • 4
  • 53
  • 51
7

Most browsers do, however Internet Explorer 9 has issues with it where it wont run any javascript unless you have the debug window open. Took us hours to find the solution to that problem.

Menztrual
  • 40,867
  • 12
  • 57
  • 70
  • 1
    With IE9 you need to jiggle the handle, so to speak, to get the console working properly. Add this to your JS: `if (typeof console.log === "object" && Function.prototype.bind && console) { ["log","info","warn","error","assert","dir","clear","profile","profileEnd"] .forEach(function (method) { console[method] = this.call(console[method], console); }, Function.prototype.bind); }` There's more about it in [this blog post](http://patik.com/blog/complete-cross-browser-console-log/). – craigpatik Nov 21 '13 at 17:54
  • Note that the above snippet will throw an error in IE8 if you include Modernizr since Modernizr includes a buggy polyfill for `Function.prototype.bind`. – craigpatik Nov 21 '13 at 17:56
1

Here is a workaround for when console.log() is not available. You have to retrieve the console.logs yourself.

  if (!window.console) window.console = {};
  if (!window.console.log) 
  {
    window.console.logs = [];
    window.console.log = function (string)
    {
      window.console.logs.push(string);
    };
  }
Artjom B.
  • 61,146
  • 24
  • 125
  • 222
Kawd
  • 4,122
  • 10
  • 37
  • 68
0

Although not all browsers support that, it can be accomplished with a small chunk of code.

In his book, "Secrets of Javascript Ninja", John Resig (creator of jQuery) has a really simple code which will handle cross-browser console.logissues. He explains that he would like to have a log message which works with all browsers and here is how he coded it:

 function log() {
  try {
     console.log.apply(console, arguments);
  } catch(e) {
  try {
     opera.postError.apply(opera, arguments);
  }
  catch(e) {
     alert(Array.prototype.join.call( arguments, " "));
  }
}
Ray Toal
  • 86,166
  • 18
  • 182
  • 232
ambodi
  • 6,116
  • 2
  • 32
  • 22