Open In App

JavaScript TypedArray.of() Method

Improve
Improve
Like Article
Like
Save
Share
Report

The TypedArray.of() method is used to create a new array from a variable number of arguments that are passed to it.

Syntax:

TypedArray.of( el0, el1, el2, ...elN )

Parameters: This method accepts a variable number of elements, which are used to create the Int16Array array.

TypedArray can contain any of the below-mentioned values:

Int8Array
Uint8Array
Uint8ClampedArray
Int16Array
Uint16Array
Int32Array
Uint32Array
Float32Array
Float64Array
BigInt64Array
BigUint64Array

Return Value: This method returns a new TypedArray instance.

The below example illustrates the TypedArray.of() Method in JavaScript:

Example 1: In this example, the values passed are the character values that are converted to Int16 by the method.

JavaScript




<script>
    // Create a Uint16Array by using
    // the Int16Array.of() method
    let int16Arr = new Int16Array;
    int16Arr =
      Int16Array.of('31', '52', '16', '80', '24');
      
    // Print the result 
    console.log(int16Arr);
</script>


Output:

[31, 52, 16, 80, 24]

Example 2: In this example, the values passed are the Integer values which are converted to Int16 by the method. The value -499999 is converted to 15537.

JavaScript




<script>
    // Create a Uint16Array by using
    // the Int16Array.of() method
    let int16Arr = new Int16Array;
      
    // Accepts the Int16 values
    int16Arr =
      Int16Array.of(-49999, 5, 7, 799, 8); 
      
    // Print the result 
    console.log(int16Arr); 
</script>


Output:

[15537, 5, 7, 799, 8]

Example 3: In this example, the values passed are the character values that are converted to Uint16 by the method.

JavaScript




<script>
    // Create a Uint16Array by using
    // the Uint16Array.of() method
    let uint16Arr = new Uint16Array;
    uint16Arr =
      Uint16Array.of('31', '50', '26', '60', '24');
      
    // Print the result 
    console.log(uint16Arr); 
</script>


Output:

[31, 50, 26, 60, 24]

Example 4:In this example, the values passed are the Integer values which are converted to Uint16 by the method. The value -499999 is thus converted to 24289.

JavaScript




<script>
    // Create a Uint16Array by using
    // the Uint16Array.of() method
    let uint16Arr = new Uint16Array;
      
    // Accepts only the Uint16 values
    uint16Arr = 
      Uint16Array.of(-499999, 5, 7, 799, 8);
      
    // Print the result 
    console.log(uint16Arr);
</script>


Output:

[24289, 5, 7, 799, 8]

Supported Browsers:

  • Chrome
  • Edge
  • Firefox
  • Opera
  • Safari


Last Updated : 10 Jan, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads