bun icon indicating copy to clipboard operation
bun copied to clipboard

`console.log()` doesn't work as in node/chrome when trying to log an array with an added custom property

Open nikitavoloboev opened this issue 2 years ago • 4 comments

const arr = ['a', 'b', 'c'];
arr['five'] = 5

console.log(arr)

Above program if run with node returns:

[ 'a', 'b', 'c', five: 5 ]

With bun, it returns:

[ "a", "b", "c" ]

Was going through a course on ExecuteProgram.com and they had it as one of their lessons to teach that arrays in JS are also objects.

Actually in safari if you run above, it will return what bun returns but chrome will return what node does. This makes sense given how zig is built on JavaScriptCore. Just found it interesting. I guess this can be closed. JavaScriptCore probably chose to do it this way for perf reasons as no sane person would modify arrays with custom properties.

nikitavoloboev avatar Jul 06 '22 20:07 nikitavoloboev

I guess it's more logic, cause five is not an entry like a or b, it's a new prop added to arr.

AbdelrhmanSaid avatar Jul 06 '22 20:07 AbdelrhmanSaid

Yea going to close this as bun is using console.log() from JavaScriptCore so it's a question to them rather than bun.

nikitavoloboev avatar Jul 06 '22 20:07 nikitavoloboev

bun implements console.log -- it's a bug in bun's implementation that it doesn't look at array properties

Jarred-Sumner avatar Jul 06 '22 20:07 Jarred-Sumner

it's a bug in bun's implementation that it doesn't look at array properties

But if you run above code snippet in safari console, you also won't see properties.

image

nikitavoloboev avatar Jul 06 '22 20:07 nikitavoloboev