Skip to content
Related Articles

Related Articles

JavaScript Uint8Array.of() Method
  • Last Updated : 07 Sep, 2020

The Uint8Array array represents an array of 8-bit unsigned integers. The values are initialized by 0. Elements in the array can be referenced by the object’s methods or using a standard syntax (by bracket notation).

The Uint8Array.of() method creates a new typed array from a variable number of arguments.

Syntax:

Uint8Array.of(el0, el1, el2, .., eln)

Parameters:

  • n-elements: This method accepts the number of elements, which are basically the element for which the array is to be created.

Return Value: This method returns a new Uint8Array instance.



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

JavaScript

filter_none

edit
close

play_arrow

link
brightness_4
code

<script> 
    // Creating a Uint8Array from a array by  
    // Creating the array from the Uint8Array.of() method
    let uint8Arr = new Uint8Array;
    uint8Arr = Uint8Array.of('41', '51', '56', '8', '24');
        
    // Printing the result 
    console.log(uint8Arr); 
</script>

chevron_right


Output:

Uint8Array(5) [41, 51, 56, 8, 24]

Example 2: In this example, the values passed are the int values that are converted to Uint8 by the method. -49999 and 799 converted to 177 and 31 respectively.

JavaScript

filter_none

edit
close

play_arrow

link
brightness_4
code

<script> 
  
    // Create a Uint8Array from a array by  
    // Creating the array from the Uint8Array.of() method
    let uint8Arr = new Uint8Array;
  
    // Accepts the uint8 values
    uint8Arr = Uint8Array.of(-49999, 5, 7, 799, 8); 
  
    // Print the result 
    console.log(uint8Arr); 
</script>

chevron_right


Output:

Uint8Array(5) [177, 5, 7, 31, 8]

full-stack-img

My Personal Notes arrow_drop_up
Recommended Articles
Page :