9

Possible Duplicate:
What’s the difference between console.dir and console.log?

I have recently learnt the existence of console.dir().

After looking through MDN, I did not clearly understand what is the real difference between this and console.log. They both show the same output (but .dir shows some properties), is that it?

Which function should I use when debugging/developping?

EDIT: I just found out an existing question which answers my thoughts: What's the difference between console.dir and console.log?

Community
  • 1
  • 1
Jeff Noel
  • 7,500
  • 4
  • 40
  • 66

1 Answers1

13

The way the information is presented is different. For example, in Firebug, if I do this:

a = { foo: "foo", bar: "bar" };

And then I do:

console.log(a)

I get:

Object { foo="foo", bar="bar"}

If I do this:

console.dir(a)

I get:

bar    "bar"
foo    "foo"

If I had nested objects, I would have the little twisty controls (MDN calls them "disclosure triangles") so that I could easily dig deeper into the object properties.

Depending on the tools you are using, YMMV.

Matt Burland
  • 44,552
  • 18
  • 99
  • 171
  • Thank you, I will rely more on `console.dir` from now on. It's quite funny that last week I was struggling with `console.log`: Chrome wouldn't show me the content of an object (nor its properties). – Jeff Noel Jan 07 '13 at 20:07
  • Thanks for a concise and clear explanation :) – Bharat Aug 14 '15 at 14:12