Skip to content
Related Articles

Related Articles

TypedArray Introduction
  • Last Updated : 07 Sep, 2020

TypedArray demonstrates an array-like view of a binary data buffer. They are introduced in ECMAScript version 6 to handle binary data. There is no keyword reserved ‘TypedArray’, nor is there a directly visible TypedArray constructor.

There are several types of TypedArray shown below with there range, size, web IDL Type, Equivalent C Type:

Type

Range Value

Size (Bytes)



Web IDL Type

Equivalent C type

Int8Array

-128 to 127

1

byte

int8_t

Uint8Array

0 to 255

1

octet

uint8_t

Uint8ClampedArray

0 to 255

1

octet

uint8_t

Int16Array



-32768 to 32767 

short

int16_t

Uint16Array

0 to 65535 

unsigned short 

uint16_t

Int32Array

-2147483648 to 2147483647 

long

int32_t

Uint32Array

0 to 4294967295 

unsigned long 

uint32_t

Float32Array

1.2×10^-38 to 3.4×10^38 

unrestricted float 

float

Float64Array

5.0×10^-324 to 1.8×10^308 

unrestricted double 

double

BigInt64Array

-2^63 to 2^63-1 

bigint

int64_t (signed long long)

BigUint64Array

0 to 2^64-1 

bigint 

uint64_t (unsigned long long)

Constructor: This object cannot be instantiated directly. Instead, you have to create an instance of an array of a particular type, such as an UInt8Array or a BigInt64Array
When creating an instance of a TypedArray(e.g. Int8Array), an Array Buffer instance is also created internally in the memory or, if an ArrayBuffer object is given as a constructor argument, then that argument is used instead. 
We can create the instance of TypedArray by using the ‘new’ keyword with the respective constructor.

Syntax:

new TypedArray();
// Or
new TypedArray(length);
// Or
new TypedArray(typedArray);
// Or
new TypedArray(object);
// Or
new TypedArray(buffer [, byteOffset [, length]]);

TypedArray can be any type mention above.

Parameters: Below are the parameter which TypedArray can take:

  • length: The size of ArrayBuffer to create.
  • typedArray: Type of array to be created.
  • object: When called with the object argument, a new typed array instance is created.
  • buffer, byteOffset, length: When called with a buffer,byteOffset, and a length argument, a new typed array view is created that is used to view the specified ArrayBuffer.

Instantiating a TypedArray: Below are some examples that show how to instantiate a TypedArray.

Example 1:

Javascript

filter_none

edit
close

play_arrow

link
brightness_4
code

const buffer = new ArrayBuffer(8);
  
// Initiating using the constructor
const uint8 = new Uint8Array(buffer);
  
// Output is 8 as we initiated the length with 8
console.log(uint8.length);

chevron_right


Output:
 

Example 2: 

Javascript

filter_none

edit
close

play_arrow

link
brightness_4
code

const int8 = new Int8Array([0, 0, 0, 0]);
  
int8.fill(4, 1, 3);
  
console.log(int8);

chevron_right


Output:
 

Example 3: Checking if the values are contained inside the TypedArray.

Javascript

filter_none

edit
close

play_arrow

link
brightness_4
code

const int8 = new Int8Array([10, 20, 30, 40, 50]);
  
console.log(int8.includes(20));
  
console.log(int8.includes(20, 3));

chevron_right


Output:

full-stack-img

My Personal Notes arrow_drop_up
Recommended Articles
Page :