170
var functor=function(){
    //test
}

functor.prop=1;

console.log(functor);

this only show the function part of the functor, cannot show the properties of the functor in console.

Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158
lovespring
  • 19,051
  • 42
  • 103
  • 153

9 Answers9

271

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

console.dir(functor);

Prints a JavaScript representation of the specified object. If the object being logged is an HTML element, then the properties of its DOM representation are printed [1]


[1] https://developers.google.com/web/tools/chrome-devtools/debug/console/console-reference#dir

Mosh Feu
  • 28,354
  • 16
  • 88
  • 135
Nick Craver
  • 623,446
  • 136
  • 1,297
  • 1,155
  • 1
    It should be noted that just printing `varName` in Chrome console and hitting Enter gives the same effect as `console.dir(varName)`. – Vadzim Oct 16 '17 at 14:58
  • @Vadzim I don't think that's true (anymore). Try it with an HTML element you selected for example. – Niels Bom Nov 29 '22 at 16:12
127

You might get better results if you try:

console.log(JSON.stringify(functor));
BastiBen
  • 19,679
  • 11
  • 56
  • 86
  • this answer is great but I think doesn't work with the sample above, tried in a new tab and returns undefined – aitorllj93 Apr 15 '15 at 11:52
  • 3
    With all due respect to this answer, eventually it returns a string representing the object, and not a "browseable" object in the console, like the question is all about here. True, if you run this output string through JSON.parse, it will return to its object-format, but then the console will still show it a ".toString()" and we're back to square one. The answer here with the use of "console.dir" is the best fit for the question in hand. – TheCuBeMan Mar 02 '16 at 11:48
38

You might get even better results if you try:

console.log(JSON.stringify(obj, null, 4));
Trident D'Gao
  • 18,973
  • 19
  • 95
  • 159
14
var gandalf = {
  "real name": "Gandalf",
  "age (est)": 11000,
  "race": "Maia",
  "haveRetirementPlan": true,
  "aliases": [
    "Greyhame",
    "Stormcrow",
    "Mithrandir",
    "Gandalf the Grey",
    "Gandalf the White"
  ]
};
//to console log object, we cannot use console.log("Object gandalf: " + gandalf);
console.log("Object gandalf: ");
//this will show object gandalf ONLY in Google Chrome NOT in IE
console.log(gandalf);
//this will show object gandalf IN ALL BROWSERS!
console.log(JSON.stringify(gandalf));
//this will show object gandalf IN ALL BROWSERS! with beautiful indent
console.log(JSON.stringify(gandalf, null, 4));
Kean Amaral
  • 5,503
  • 2
  • 16
  • 9
9

this worked perfectly for me:

for(a in array)console.log(array[a])

you can extract any array created in console for find/replace cleanup and posterior usage of this data extracted

domSurgeon
  • 91
  • 1
  • 3
5

I made a function of the Trident D'Gao answer.

function print(obj) {
  console.log(JSON.stringify(obj, null, 4));
}

How to use it

print(obj);
Jens Törnell
  • 23,180
  • 45
  • 124
  • 206
0

I wrote a function to conveniently print things to the console.

// function for debugging stuff
function print(...x) {
    console.log(JSON.stringify(x,null,4));
}

// how to call it
let obj = { a: 1, b: [2,3] };
print('hello',123,obj);

will output in console:

[
    "hello",
    123,
    {
        "a": 1,
        "b": [
            2,
            3
        ]
    }
]
John Henckel
  • 10,274
  • 3
  • 79
  • 79
0

With modern browsers, console.log(functor) works perfectly (behaves the same was a console.dir).

akim
  • 8,255
  • 3
  • 44
  • 60
0

Another method is to wrap function into an array:

console.log( [functor] );
vanowm
  • 9,466
  • 2
  • 21
  • 37