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

07 November 2010   2 comments   Javascript

Mind That Age!

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

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;
}
Follow @peterbe on Twitter

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 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
Related by Text:
Don't that this or bind 12 April 2016
window.onload from before 07 June 2005
Chainable catches in a JavaScript promise 05 November 2015
Catching a carriage return in bash 23 October 2006
parametrize_url() adding parameters to URLs 14 January 2005