views:

13306

answers:

6

How can I print a message to the error console, preferably including a variable? e.g., something like

print('x=%d', x);
+13  A: 

Install Firebug and then you can use console.log(...) and console.debug(...), etc (see the docs for more).

Dan
Unfortunately, this will cause an error in IE though. So you have to comment it out before deployment, or include a library that traps it.http://getfirebug.com/lite.html
Shermozle
@Dan: The WebKit Web Inspector also supports the FireBug console API
olliej
why is this the accepted answer? he didn't ask how to write to the firebug console, he asked how to write to the error console. not being a dick or anything, just pointing it out.
matt lohkamp
Oh come on. If you're a professional developer you'll know full well that answering questions like is is as much about guessing what the person REALLY wants than about answering their question fully. Not that I'm saying "I'm sooo good I know what you're thinking!", I simply happened to guess right.
Dan
+3  A: 

If you are using Firebug and need to support IE, Safari or Opera as well, Firebug Lite adds console.log() support to these browsers.

Devon
Wow.. Firebug Lite is awesome
Dexter
+1  A: 

The WebKit Web Inspector also supports FireBug's console API (just a minor addition to Dan's answer above)

olliej
+6  A: 

One good way to do this that works cross-browser is outlined in http://www.sitepoint.com/blogs/2008/08/22/debugging-javascript-throw-away-your-alerts/

Ian Oxley
throw() is a great suggestion - this should be the chosen answer.
matt lohkamp
Yeah, it's cross-browser and it does the job. Upped.
ArtBIT
+5  A: 

Exceptions are logged into the javascript console. You can use that if you want to keep Firebug disabled.

function log(msg) {
    setTimeout(function() {
        throw new Error(msg);
    }, 0);
}

Usage:

log('Hello World');
log('another message');
Ivo Danihelka
A: 
codegen
You should post this as a new question, expanding on what you're trying to do.
josh3736