How does the node.js REPL stringify the objects that it prints? I became curious after running into this oddity with an array containing a key-value pair (?!):
> var arr = [1]
undefined
> arr
[ 1 ]
> arr.key = "value"
'value'
> arr
[ 1, key: 'value' ]
The typical functions don't appear to be generating exactly [ 1, key: 'value' ]
.
> arr.toString()
'1'
> JSON.stringify(arr)
'[1]'
> require("util").inspect(arr, {showHidden: true})
'[ 1, [length]: 1, key: \'value\' ]'
The last one's almost it, but has an additional [length]
(to clarify, the quotes obviously don't matter). I'm running node v0.10.33.
You're almost there; util.inspect returns a string and is called without
showHidden
:If you log a string to the console, it'll be wrapped in quotes, but
console.log
does not do this. Writeinspect
's output to a file if you want to verify that it's exactly the same asconsole.log
's output:In your console:
Regarding the first paragraph:
It's not that weird - arrays are objects, so it's perfectly valid. But it might be confusing to other people looking at your code. :-)
Doing that is not much different from this:
It'll look just like your array example if you were to log this.