Skip to content
Related Articles

Related Articles

JavaScript | Uint8Array.from() Method
  • Last Updated : 28 Jan, 2020
GeeksforGeeks - Summer Carnival Banner

The Uint8Array array represents an array of 8-bit unsigned integers. By default, the contents of Uint8Array are initialized to 0.

The Uint8Array.from() method is used to create a new Uint8Array from an array-like or iterable object. So when you want to convert an arrayLike or iterable object then you can be used this function by passing the object as a parameter to this function along with map function and value used for map function if needed.


Uint8Array.from( source, mapFn, thisArg )

parameters: This method accept three parameters as mentioned above and described below:

  • source: This parameter contains an array-like or iterable object which is used to convert to a Uint8Array object.
  • mapFn: It is an optional parameter which is Map function to call on every element of the Uint8Array array.
  • thisArg: It is an optional parameter which store the value to use as this when executing mapFn.

Return Value: This method returns a new Uint8Array instance.

Below example illustrate the working of Uint8Array.from() method in JavaScript:

Program 1:

// Create a Uint8Array from a string like structure
var  array = Uint8Array.from('45465768654323456');
// Print the result
4, 5, 4, 6, 5, 7, 6, 8, 6, 5, 4, 3, 2, 3, 4, 5, 6

Program 2:

// Create a Uint8Array from a array by adding
// 3 to each number using function
var  array = Uint8Array.from([1, 2, 3, 4, 5, 6], z => z+3);
// Print the result
4, 5, 6, 7, 8, 9



My Personal Notes arrow_drop_up
Recommended Articles
Page :