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
<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> |
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
<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> |
Output:
Uint8Array(5) [177, 5, 7, 31, 8]
Supported Browsers:
- Chrome
- Edge
- Firefox
- Opera
- Safari