FireBug's Console For Debugging Javascript Data (Thanks Ray Camden)

Posted January 14, 2009 at 8:52 AM by Ben Nadel

Tags: Javascript / DHTML

I love FireBug. I think it is the most amazing FireFox plugin, hands down. I don't think I go through more than an hour of development work without using it; it makes debugging CSS and XHTML issues mere child's play. The way it breaks down CSS rules and shows you the cascading nature of the various rules as they are applied to a selected DOM element and which CSS files each rule came from... you can't even choose the right words to fully express the usefulness of that.

This morning, thanks to Ray Camden, FireBug just got exponentially cooler! He introduced me to FireBug's "console" Javascript object. Of course, I've heard of console.log() before in passing, but I just never looked into it. To be honest, FireBug is such a huge plugin that once I started to use the CSS, XHTML Inspection, and NET activity features, I simply stopped looking into its other features; I figured that I had already squeezed all the tasty goodness out of it.

But that has all changed! The Console object has two methods that I think will revolutionize the way I debug my Javascript. Those methods are log() and dir(). Log() prints a message to FireBug's console. Dir() prints an interactive object explorer to the console. Let's take a quick look at a small example:

  • <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  • <html>
  • <head>
  • <title>FireBug Console Testing</title>
  •  
  • <script type="text/javascript">
  •  
  • // Define complex object.
  • var objGirl = {
  • Name: "Molly",
  • Hair: "Brunette",
  • Eyes: "Brown",
  • BestQualities:
  • [
  • "Smile",
  • "Laugh"
  • ]
  • };
  •  
  • // Debug data in FireBug.
  • console.log( objGirl );
  • console.dir( objGirl );
  •  
  • </script>
  • </head>
  • <body>
  •  
  • <h1>
  • FireBug Console Testing
  • </h1>
  •  
  • </body>
  • </html>

As you can see in this example, we are building a complex Javascript object. We then pass it to both the log() and dir() methods. This results in the following FireBug console output:

 
 
 
 
 
 
FireBug's Console Output For Complex Javascript Objects. 
 
 
 

Awesome! I can't get over how easy this is going to make looking at complex Javascript objects. When I think of all the times that I've tried to create massive data strings for alert() calls by using a for-in object loop and string concatenation. So many wasted hours!

FireBug, you are amazing. Ray Camden, you rock - thanks for showing this to me.

NOTE: Running console.log( console ) doesn't do anything cool... it doesn't do anything at all, in fact.




Reader Comments

Jan 14, 2009 at 9:06 AM // reply »
2 Comments

Handy! I didn't know that. But if you weren't that organised to put the console.log() and console.dir() in your code to begin with, the variable will usually be accessible in the DOM tab of FireBug.


Jan 14, 2009 at 9:16 AM // reply »
30 Comments

The Firebug console is awesome. It actually has quite a few methods which are documented here: http://getfirebug.com/console.html


Jan 14, 2009 at 9:34 AM // reply »
10,640 Comments

@Alan,

Good to know - I didn't even know the DOM tab would include user-defined Javascript objects! Your comment got me thinking:

http://www.bennadel.com/index.cfm?dax=blog:1459.view

@Nathan,

Thanks for the link.


Jan 14, 2009 at 10:36 AM // reply »
14 Comments

Console is the bomb. An absolute must for anyone doing client-side development. Their is one downside, which we hit here this morning. Be careful to remove your console statements from code prior to pushing to production. Internet Explorer (go figure) does not support console, and the console statements will error your code and stop process.


Jan 14, 2009 at 10:38 AM // reply »
10,640 Comments

@Steve,

Good point. I guess, it's probably best just to use them when something is going wrong.


Jan 14, 2009 at 2:20 PM // reply »
25 Comments

Don't forget about the 'debugger;' statement that lets you dynamically add in break points for your code. I just found that out and have been loving it ever since.


Jan 14, 2009 at 3:02 PM // reply »
10,640 Comments

@JAlpino,

Mark Drew just walked me through some debugger stuff this morning. Looks really nice.


Jan 15, 2009 at 10:51 PM // reply »
57 Comments

Very cool, thanks for sharing! I've played a bit with the JS debugger in the past, but this will definitely make it much more useful.


Sep 9, 2011 at 6:57 PM // reply »
6 Comments

You have saved me once again! I was in the alert box hell, when I found this post. Thank you!!!


Post A Comment

Comment Etiquette: Please do not post spam. Please keep the comments on-topic. Please do not post unrelated questions or large chunks of code. And, above all, please be nice to each other - we're trying to have a good conversation here.

Please review the following issues:

Author Name:


Author Email:

Author Website:

Comment:

Supported HTML tags for formatting: <strong>bold</strong>   <em>italic</em>   <code>code</code>







  • Help Wanted - Find Your Next ColdFusion Job
InVision App - Prototyping Made Beautiful With Prototyping Tools Ben Nadel's Company - Epicenter Consulting Recent Blog Comments
Feb 10, 2012 at 7:21 PM
jQuery AJAX Strips Script Tags And Inserts Them After Parent-Most Elements
Update! Instead of $(eval(options.insertAfter)).after(data['insertData']); I now use: var ajaxNode = document.createElement('span'); var parent = $(eval(options.insertAfter))[0].parentNode; ... read »
Feb 10, 2012 at 6:18 PM
jQuery AJAX Strips Script Tags And Inserts Them After Parent-Most Elements
encountered this same, what I consider, jQuery bug last week. I'm building a site in which I load some content via AJAX. This content contains Linkedin share button placeholders which Linkedin API ne ... read »
Feb 10, 2012 at 11:30 AM
Cross-Origin Resource Sharing (CORS) AJAX Requests Between jQuery And Node.js
After you understand the concepts here, this is an awesome cheatsheet for enabling CORS in just about anything http://enable-cors.org/ ... read »
JM
Feb 10, 2012 at 9:10 AM
My Safari Browser SQLite Database Hello World Example
@Amy, Here is a very good tutorial on how to use JOIN: http://www.sqltutorial.org/sqljoin-innerjoin.aspx ... read »
Feb 10, 2012 at 4:42 AM
Building A Twitter-Inspired RESTful API Architecture In ColdFusion
This is great, very useful Ben. I spotted a small typo in the api.cgm listing: <cfthrow type="Unauthroized" /> Cheers Stefan ... read »
Feb 9, 2012 at 10:35 PM
CFDirectory Filtering Uses Pipe Character For Multiple Filters (Thanks Steve Withington)
I was wondering if there would be a filter you could apply so that you got everything but what you included in the filter. As in show me all docs that are not a .pdf. ... read »
Feb 9, 2012 at 10:29 PM
Learning ColdFusion 9: Application-Specific Data Sources
@Ben, No offence, but if people were really wanting advanced features they would be using a platform like ASP.NET MVC. CFML is so structurally compromised as a tag-based scripting language that ... read »
Feb 9, 2012 at 10:03 PM
Subversion - Cleanup Failed To Process The Following Paths
@Leviaguirre, do you still have problems with this? ... read »