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

7 November 2010   2 comments   JavaScript

Mind that age!

This blog post is 12 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