425

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?

Drew Noakes
  • 300,895
  • 165
  • 679
  • 742
  • 75
    Try `console.log([1,2])` and `console.dir([1,2])` and you will see the difference. – Felix Kling Aug 14 '12 at 14:25
  • In firebug the contents of an object logged with `console.dir` doesn't change, so it makes a big difference. – Eugene Yarmash Jul 26 '13 at 09:53
  • 3
    Be carefull with `console.dir()` : this feature is [non-standard](https://developer.mozilla.org/docs/Web/API/Console/dir) ! So do not use it on production ;) – fred727 Sep 10 '15 at 07:33
  • 2
    This question's URL is shown in the [image](http://i.imgur.com/DozDcYR.png) at [Mozilla Developer Network - Console.log() - Difference with console.dir()](https://developer.mozilla.org/en-US/docs/Web/API/Console/log#Difference_with_console.dir()). – user7393973 Mar 16 '17 at 14:18
  • 1
    @user7393973 nice find! Actually, the image is from [my answer](http://stackoverflow.com/a/19269945/24874) below, so was actually captured on my laptop. It's nice to give something back to MDN. What a great resource it is. – Drew Noakes Mar 16 '17 at 17:01
  • Origin of `dir` method name can also be helpful in understanding the difference - [What does the `dir` in `console.dir` stand for?](https://stackoverflow.com/q/37555969/465053) – RBT Sep 22 '18 at 00:21
  • 1
    @fred727 `console.dir()` is standard now. – theking2 May 03 '23 at 16:38

9 Answers9

411

In Firefox, these function behave quite differently: log only prints out a toString representation, whereas dir prints out a navigable tree.

In Chrome, log already prints out a tree -- most of the time. However, Chrome's log still stringifies certain classes of objects, even if they have properties. Perhaps the clearest example of a difference is a regular expression:

> console.log(/foo/);
/foo/

> console.dir(/foo/);
* /foo/
    global: false
    ignoreCase: false
    lastIndex: 0
    ...

You can also see a clear difference with arrays (e.g., console.dir([1,2,3])) which are logged differently from normal objects:

> console.log([1,2,3])
[1, 2, 3]

> console.dir([1,2,3])
* Array[3]
    0: 1
    1: 2
    2: 3
    length: 3
    * __proto__: Array[0]
        concat: function concat() { [native code] }
        constructor: function Array() { [native code] }
        entries: function entries() { [native code] }
        ...

DOM objects also exhibit differing behavior, as noted on another answer.

Community
  • 1
  • 1
apsillers
  • 112,806
  • 17
  • 235
  • 239
  • 14
    Don't forget that console.dir keeps a reference to the object. You probably don't want to use it extensively in production. It probably only works if the console is shown tho. – Jean-Philippe Leclerc May 08 '13 at 15:08
  • In Chromium 45 on Ubuntu, `console.log` seems to be the simplified, "Firefox" version that looks like a `toString` rather than a tree. I'm not sure yet when they changed it, but they did. – icedwater Nov 15 '15 at 08:55
  • 3
    @icedwater: Depends on whether you have the console open when you call `console.log` or open it later. Yes, really. :-) – T.J. Crowder Jun 22 '17 at 17:12
  • You can also see a clear difference with Function. In chrome, it will print Function source code with `console.log`, but with `console.dir`, you can see the `prototype`, `arguments` properties. – Tina Chen May 04 '18 at 01:03
  • 5
    Now it seems that `console.log` and `console.dir` actually return the same representation on `[1,2,3]` in Firefox. – xji Jun 18 '18 at 16:27
  • 1
    Same result on Chrome Version 91.0 – Muhammet Can TONBUL Jul 01 '21 at 08:19
170

Another useful difference in Chrome exists when sending DOM elements to the console.

Notice:

  • console.log prints the element in an HTML-like tree
  • console.dir prints the element in a JSON-like tree

Specifically, console.log gives special treatment to DOM elements, whereas console.dir does not. This is often useful when trying to see the full representation of the DOM JS object.

There's more information in the Chrome Console API reference about this and other functions.

Drew Noakes
  • 300,895
  • 165
  • 679
  • 742
  • I believe this information is from https://developer.mozilla.org/en-US/docs/Web/API/Console/log – loneshark99 Feb 20 '18 at 18:18
  • 23
    @loneshark99 actually it's the other way around. Notice the URL in their screenshot? They copied my answer. But that's fine with because that's allowed by the license on SO answers and I love MDN anyway. – Drew Noakes Feb 20 '18 at 19:35
  • 1
    Got it , that’s what I was initially thinking but then thought why they would copy from here. Makes sense . Good information – loneshark99 Feb 20 '18 at 19:37
  • 1
    @DrewNoakes legend. Moment when answer on StackOverflow becomes the official reference. LOL – Mars Robertson Jan 20 '21 at 16:19
  • @DrewNoakes shouldn't they at least credit you even though SO practices "all up for grabs" license? – m4heshd Feb 21 '21 at 09:17
  • @m4heshd interesting question. Licenses for user contributions on SO have [changed slightly over the years](https://stackoverflow.com/help/licensing). This post was made under [CC BY-SA 3.0](https://creativecommons.org/licenses/by-sa/3.0/) which does require attribution. Personally I don't mind in this instance. I'd rather this information was kept on MDN than taken down because of licensing concerns. Consider this a license excemption for MDN :) – Drew Noakes Feb 21 '21 at 21:46
  • @DrewNoakes, That's good. May your humbleness serve you well. – m4heshd Feb 21 '21 at 22:28
6

None of the 7 prior answers mentioned that console.dir supports extra arguments: depth, showHidden, and whether to use colors.

Of particular interest is depth, which (in theory) allows travering objects into more than the default 2 levels that console.log supports.

I wrote "in theory" because in practice when I had a Mongoose object and ran console.log(mongoose) and console.dir(mongoose, { depth: null }), the output was the same. What actually recursed deeply into the mongoose object was using util.inspect:

import * as util from 'util';
console.log(util.inspect(myObject, {showHidden: false, depth: null}));
Dan Dascalescu
  • 143,271
  • 52
  • 317
  • 404
4

I think Firebug does it differently than Chrome's dev tools. It looks like Firebug gives you a stringified version of the object while console.dir gives you an expandable object. Both give you the expandable object in Chrome, and I think that's where the confusion might come from. Or it's just a bug in Chrome.

In Chrome, both do the same thing. Expanding on your test, I have noticed that Chrome gets the current value of the object when you expand it.

> o = { foo: 1 }
> console.log(o)
Expand now, o.foo = 1
> o.foo = 2
o.foo is still displayed as 1 from previous lines

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

You can use the following to get a stringified version of an object if that's what you want to see. This will show you what the object is at the time this line is called, not when you expand it.

console.log(JSON.stringify(o));
sachleen
  • 30,730
  • 8
  • 78
  • 73
3

Use console.dir() to output a browse-able object you can click through instead of the .toString() version, like this:

console.dir(obj/this/anything)

How to show full object in Chrome console?

Community
  • 1
  • 1
Scorpion-Prince
  • 3,574
  • 3
  • 18
  • 24
2

From the firebug site http://getfirebug.com/logging/

Calling console.dir(object) will log an interactive listing of an object's properties, like > a miniature version of the DOM tab.

Dries Marien
  • 211
  • 2
  • 9
2

Well, the Console Standard (as of commit ef88ec7a39fdfe79481d7d8f2159e4a323e89648) currently calls for console.dir to apply generic JavaScript object formatting before passing it to Printer (a spec-level operation), but for a single-argument console.log call, the spec ends up passing the JavaScript object directly to Printer.

Since the spec actually leaves almost everything about the Printer operation to the implementation, it's left to their discretion what type of formatting to use for console.log().

SamB
  • 9,039
  • 5
  • 49
  • 56
1

Following Felix Klings advice I tried it out in my chrome browser.

console.dir([1,2]) gives the following output:

Array[2]
 0: 1
 1: 2
 length: 2
 __proto__: Array[0]

While console.log([1,2]) gives the following output:

[1, 2]

So I believe console.dir() should be used to get more information like prototype etc in arrays and objects.

guntbert
  • 536
  • 6
  • 19
Bimal
  • 865
  • 10
  • 18
1

Difference between console.log() and console.dir():

Here is the difference in a nutshell:

  • console.log(input): The browser logs in a nicely formatted manner
  • console.dir(input): The browser logs just the object with all its properties

Example:

The following code:

let obj = {a: 1, b: 2};
let DOMel = document.getElementById('foo');
let arr = [1,2,3];

console.log(DOMel);
console.dir(DOMel);

console.log(obj);
console.dir(obj);

console.log(arr);
console.dir(arr);

Logs the following in google dev tools:

enter image description here

Willem van der Veen
  • 33,665
  • 16
  • 190
  • 155