The Stack Overflow code snippet tool produces an ambiguous and, depending on your definitions, incorrect output when using the JavaScript console.log
command with an array that has missing slots, i.e. a sparse array.
I show below screen-shots of the input and output for using console.log([undefined,,4])
from within the Stack Exchange code snippet tool as well as from within the browser consoles of various common browsers.
Stack Overflow code snippet tool input:
Stack Overflow code snippet tool output:
Note that in this output the first and second elements are shown identically, i.e. as undefined
.
The identical command shows the following output in the browser consoles in Chrome, Firefox, Safari and Opera:
Chrome:
Firefox:
Safari:
Opera:
In each of these browser consoles the output is unambiguous and correct. Specifically, only the first element is reported as undefined
and the element with index 1 is shown as missing/non-existent. In contrast, the output from within the code snippet tool is at least confusing/misleading if not technically incorrect (depending on how you define what the output of console.log
should be).
I believe that the output of console.log
in the code snippet tool should mirror the output from the browser consoles with respect to reporting missing slots in JavaScript sparse arrays, both because it will be clearer and because it will be consistent with browser console output.
Note that the above analysis did not include Internet Explorer 11 which has its own bug related to this issue. Just so responders don't waste their energy pursuing this, I show the IE11 bug here, when I try to show the same thing in the IE11 console. Note the doubling up of the element with index 2, which is reported as both undefined
and as 4
, which is clearly wrong.