The Lumber Room

"Consign them to dust and damp by way of preserving them"

Firebug “console is undefined”

with 2 comments

If you’re using Firebug and don’t want to bother removing or commenting out all the console.log debug messages for users who aren’t, put this at the top:

if(typeof(console) === "undefined" || typeof(console.log) === "undefined") 
    var console = { log: function() { } };

This reminds me of the trick I use for C/C++ of putting debugging statements inside a macro:

D(cerr<<"The number is "<<n<<endl;);

where the macro D is defined as

#define D(A) A

when you want the debugging code to run, and

#define D(A) 

when you don’t. :-)

(The final semicolon after the D() is for Emacs to indent it reasonably.)

Update: Of course, the above are just hacky hacks to save typing. The “right” way for conditional C code is usually to use #ifdef DEBUG and the like, and the right way around the Firebug problem is to use something more general like this code at metamalcolm.

Written by S

Wed, 2009-08-05 at 14:32:15

2 Responses

Subscribe to comments with RSS.

  1. […] programming. Tagged: FireBug, javascript, programming. Leave a Comment This is a response to Shreevatsa’s post of the same […]

  2. For some reason I found this didn’t seem to want to work if you add all the parts of Firebug console into it, so ended up with:

    if (typeof(console) === “undefined” ||
    typeof(console.log) === “undefined” ||
    typeof(console.error) === “undefined”) {

    var console = new Object();
    console.log = function(fakeinput) { };
    console.error = function(fakeinput) { }; = function(fakeinput) { };
    console.debug = function(fakeinput) { };
    console.warn = function(fakeinput) { };


    …which does seem to work.

    Original only affected however if calling more than one of the console types, eg:


    …whcih will then tell me that console.error() is not defined. Not worked out why. Blooming J/Scripts. :)



    Fri, 2009-11-27 at 11:53:53

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: