This is a kind of dumb question but I had an error in my code because I was adding values to a Uint16Array
at an out of range index. But the JS engine did not raise any error, it seems it just did not do anything with the additional values.
For example :
>> var uint16 = new Uint16Array(2);
>> uint16[0] = 42;
>> uint16[1] = 32;
>> uint16[2] = 12;
>> uint16
Uint16Array(2) [42, 32]
A standard JS array would have appended the new value and increase the array size.
>> var arr = new Array(2);
>> arr[0] = 42;
>> arr[1] = 32;
>> arr[2] = 12;
>> arr
(3) [42, 32, 12]
Does anybody know why we have this behavior on Uint16Array
and why it does not raise any kind of out of range exception ?
Because it's defined this way. Maybe it will change, but for now it won't raise and exception if you try to access (read or write) at an out of range index
However, getting or setting indexed properties on typed arrays will not search in the prototype chain for this property, even when the indices are out of bound.
You will find more informations about TypedArray https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray
And bonus: you don't have the usual method like push, pop...
with this kind of array.