I have been experimenting with the const
keyword in JavaScript as I learn it. I realized that I can add items to an object
or an array
.
For arrays, when I add to an index that isn't right after the end, I end up with empty items
. When I log one of the values it is says undefined
. How can I get the log display to show undefined
.
const arr = [0, 1];
console.log('Expect: [0, 1] Result = ', arr);
arr[1] = 'a';
console.log('Expect: [0, \'a\'] Result = ', arr);
arr[2] = 'c';
console.log('Expect: [0, \'a\', \'c\'] Result = ', arr);
arr[8] = 3;
console.log(arr);
console.log('Expect: [0, \'a\', <5 empty items>, 3] Result = ', arr);
console.log(arr[7]);
For node
, the last two lines give:
Expect: [0, 'a', <5 empty items>, 3] Result = [ 0, 'a', 'c', <5 empty items>, 3 ]
undefined
How could I get the log to look like:
[0, 'a', undefined, undefined, undefined, undefined, undefined, 3]
EDIT
Note that I am not trying to replace the values with say zero. I just want to see the array with the values it contains, just like how the other elements show up.
I tried the Chrome console and it also shows empty
but empty × 5
rather than <5 empty items>
.
Expect: [0, 'a', <5 empty items>, 3] Result = (9) [0, "a", "c", empty × 5, 3]