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

In Chrome the console object defines two methods that seem to do the same thing:

console.log(...)
console.dir(...)

I read somewhere online that dir takes a copy of the object before logging it, whereas log just passes the reference to the console, meaning that by the time you go to inspect the object you logged, it may have changed. However some preliminary testing suggests that there’s no difference and that they both suffer from potentially showing objects in different states than when they were logged.

Try this in the Chrome console (Ctrl+Shift+J) to see what I mean:

> o = { foo: 1 }
> console.log(o)
> o.foo = 2

Now, expand the [Object] beneath the log statement and notice that it shows foo with a value of 2. The same is true if you repeat the experiment using dir instead of log.

My question is, why do these two seemingly identical functions exist on console?

9 Answers
9

Leave a Comment