Skip to content

Bug in console.log ? #11651

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
LakshmiSwethaG opened this issue Mar 2, 2017 · 6 comments
Closed

Bug in console.log ? #11651

LakshmiSwethaG opened this issue Mar 2, 2017 · 6 comments
Labels
console Issues and PRs related to the console subsystem. question Issues that look for answers. util Issues and PRs related to the built-in util module.

Comments

@LakshmiSwethaG
Copy link
Contributor

I have this code. As you can see, the stringified version of the obejct, and the representation used by console.log are different. Is it a bug, or a limitation in the console.log?

function parse() {
var cont = [];
var obj = {};
cont.push(obj);
var arr = [];
obj['sw'] = arr;
arr.push(1);
return cont;
}

var obj = {};
var a = 'ee';
obj[a] = parse();
console.log(JSON.stringify(obj));
console.log(obj);


Output:

{"ee":[{"sw":[1]}]}
{ ee: [ { sw: [Object] } ] }  
@mscdex mscdex added console Issues and PRs related to the console subsystem. question Issues that look for answers. labels Mar 2, 2017
@mscdex
Copy link
Contributor

mscdex commented Mar 2, 2017

console.log() uses util.inspect(). That is the output you're seeing.

@mscdex mscdex closed this as completed Mar 2, 2017
@vsemozhetbyt
Copy link
Contributor

vsemozhetbyt commented Mar 2, 2017

@mscdex Simplified code:

const obj1 = { prop: [{ prop: [1] }] };
const obj2 = { prop:  { prop: [1] }  };
const obj3 =          { prop: [1] };

console.log(JSON.stringify(obj1));
console.log(JSON.stringify(obj2));
console.log(JSON.stringify(obj3));

console.log(obj1);
console.log(obj2);
console.log(obj3);
{"prop":[{"prop":[1]}]}
{"prop":{"prop":[1]}}
{"prop":[1]}
{ prop: [ { prop: [Object] } ] }
{ prop: { prop: [ 1 ] } }
{ prop: [ 1 ] }

Why 1 becomes Object in the obj1?

@addaleax
Copy link
Member

addaleax commented Mar 2, 2017

Why 1 becomes Object in the obj1?

It’s [1] that becomes [Object] to show an object that util.inspect didn’t inspect because it’s nested too deeply.

I agree, this is confusing and we could at least turn it into [Array] or even [Array with ${n} element(s)] instead.

@vsemozhetbyt
Copy link
Contributor

It seems the depth inspect option matters.

const util = require('util');

const obj1 = { prop: [{ prop: [1] }] };
const obj2 = { prop:  { prop: [1] }  };
const obj3 =          { prop: [1] };

console.log(JSON.stringify(obj1));
console.log(JSON.stringify(obj2));
console.log(JSON.stringify(obj3));

console.log(util.inspect(obj1, { depth: null }));
console.log(util.inspect(obj2, { depth: null }));
console.log(util.inspect(obj3, { depth: null }));
{"prop":[{"prop":[1]}]}
{"prop":{"prop":[1]}}
{"prop":[1]}
{ prop: [ { prop: [ 1 ] } ] }
{ prop: { prop: [ 1 ] } }
{ prop: [ 1 ] }

@vsemozhetbyt
Copy link
Contributor

Oh, and the square brackets are also a bit confusing here: [Object] seems to be an array with 1 Object.

@LakshmiSwethaG
Copy link
Contributor Author

@mscdex Thanks for the quick response.
I got my doubt cleared.
I tried with util.inspect() options: depth:null and showhidden:true, and it worked.
console.log(util.inspect(obj, { showHidden: true, depth: null }));

@addaleax addaleax added the util Issues and PRs related to the built-in util module. label Mar 26, 2017
addaleax added a commit to addaleax/node that referenced this issue Mar 26, 2017
Prefer `[Array]` over `[Object]` because the latter is confusing.

Ref: nodejs#11651
PR-URL: nodejs#12046
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
console Issues and PRs related to the console subsystem. question Issues that look for answers. util Issues and PRs related to the built-in util module.
Projects
None yet
Development

No branches or pull requests

4 participants