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

07 November 2010   2 comments   Javascript

Powered by Fusion×

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++)
    console.log(arguments[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;
}

Comments

kioopi
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.
Thank you for posting a comment

Your email will never ever be published


Related posts

Previous:
In Django, how much faster is it to aggregate? 27 October 2010
Next:
Worst Flash site of the year 2010 08 November 2010
Related by keywords:
Why I gave up on JQuery UI's autocomplete 20 October 2010
What makes my website slow? DNS 23 October 2009
Shout-out to eventlog 30 October 2014
apple.com/store doesn't work in Firefox (on Linux) 19 March 2008
ReCSS a tool to reload the CSS without reloading the whole page 31 January 2006
YSlow grade A (96) but not with doubts 06 August 2007
Associative arrays 21 March 2007
Messed up columns in Django Admin 16 October 2009
Review: Django 1.1 Testing and Debugging 20 May 2010
Making output stay on stdout 18 May 2010