An array in JavaScript is a special type of object, which contains an indexed set of objects. The
values in an array, which may be of different types, can be accessed using the []
-operator.
An array has a length
property, which returns the value of
the largest used index plus one.
Arrays in JavaScript is much more dynamic than arrays in C++ or Java; Arrays automatically grows to
your needs and arrays can even be sparse (unused elements in an array will have the value
undefined
). An array also has a number of useful methods such as push()
,
pop()
and sort()
.
When working with data used for WebGL you need to be able to known the data type (such as 32-bit floating point number) and that the data is located continuous in memory to ensure efficiency. Neither of these two requirements is guaranteed by JavaScript arrays.
When WebGL was introduced a new type of array was also introduced; Typed arrays. Typed arrays works
much more like arrays in C++ or Java. The array has a given type (32 bit float or unsigned 16 bit
integer) and data are located continuous in memory and has fixed length. Examples of typed arrays are
Float32Array
and Uint16Array
and a typed array object is created using the
new operator with the array length as parameter.
The array types like Float32Array
and Uint16Array
actually works as view
on a ArrayBuffer
(which is essentially is a block of memory). You can have different views on the
same ArrayBuffer
, which can be useful when working with raw data.