166
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.

lovespring
  • 18,247
  • 41
  • 98
  • 148

9 Answers9

265

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
  • 26,720
  • 15
  • 83
  • 122
Nick Craver
  • 610,884
  • 134
  • 1,288
  • 1,151
  • 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
124

You might get better results if you try:

console.log(JSON.stringify(functor));
BastiBen
  • 19,131
  • 10
  • 52
  • 85
  • 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
31

You might get even better results if you try:

console.log(JSON.stringify(obj, null, 4));
Trident D'Gao
  • 17,538
  • 18
  • 90
  • 151
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
  • 4,575
  • 2
  • 14
  • 8
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
  • 2
3

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
  • 20,740
  • 40
  • 109
  • 183
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
  • 8,771
  • 3
  • 67
  • 74
0

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

akim
  • 7,642
  • 2
  • 41
  • 56
-5

To output obj:

console.log(obj, null, 4)