Javascript tip: nifty use of the console.log function in Firebug

07 November 2010   2 comments   Javascript

Mind that age!

This blog post is 10 years old! Most likely, its content is outdated. Especially if it's technical.

A classic blunder in Javascript client side apps is heavy use of the incredibly useful Firebug feature that is console.log() and then forgetting to remove any such debugging and thus causing Javascript errors for people without Firebug. To remedy this use this little nifty function:

function log() {
  if (window.console && window.console.log)
  for (var i = 0, l = arguments.length; i < l; i++)

That way you can do plenty of debugging and if you accidentally leave a logging line in your code it won't break anything even if they don't have Firebug installed/enabled. Also you can easily "annotate" your debugging by writing two things in one line. Like this:

function foo(bar) {
   log("bar is:", bar);
   return bar * 2;



console.log can take multiple arguments. so it's possible to just apply all arguments of the custom log function to it:

console.log.apply(console, arguments);

Peter Bengtsson

Really?! That's cool. I'm going to try that.

Your email will never ever be published

Related posts

In Django, how much faster is it to aggregate? 27 October 2010
Worst Flash site of the year 2010 08 November 2010
Related by Keyword:
Shout-out to eventlog 30 October 2014
Why I gave up on JQuery UI's autocomplete 20 October 2010
Review: Django 1.1 Testing and Debugging 20 May 2010
Making output stay on stdout 18 May 2010
What makes my website slow? DNS 23 October 2009