Search code examples
node.jsarraybuffer

node js - storing large numbers in ArrayBuffer


Consider this code:

var b = new ArrayBuffer(2);

b[0] = 1;
b[1] = 23234322442;

The number in cell number 1 is clearly larger than 1 byte, yet the ArrayBuffer has no problem storing it, and when I print b to console I can see the number is there. What's the explanation?

Thanks.


Solution

  • The ArrayBuffer object is used to represent a generic, fixed-length raw binary data buffer. You cannot directly manipulate the contents of an ArrayBuffer; instead, you create one of the typed array objects or a DataView object which represents the buffer in a specific format, and use that to read and write the contents of the buffer. more

    So, I think, when you call b[1] then you don't set second element of buffer. You simple set b.1 property of object and it can be anything (number, string, object, array).