The Wayback Machine - https://web.archive.org/web/20240118174021/https://www.geeksforgeeks.org/javascript-uint8array-from-method/
Open In App
Related Articles

JavaScript Uint8Array.from() Method

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

The Uint8Array.from() method in JavaScript is used to create a new Uint8Array from an array-like or iterable object. It allows you to transform an existing array or iterable into a new Uint8Array instance, where each element of the new array is represented as an 8-bit unsigned integer.

Syntax: 

Uint8Array.from( source, mapFn, thisArg )

Parameters:

This method accepts 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 the Map function to call on every element of the Uint8Array array.
  • thisArg: It is an optional parameter that stores the value to use as this when executing mapFn.

Return Value:

This method returns a new Uint8Array instance. 

Example 1: In this example, we will see the basic functionality of the Uint8Array array to create new Uint8array from a string of integers.

javascript




// Create a Uint8Array from a
// string like structure
let array = Uint8Array.from('45465768654323456');
 
// Display the result
console.log(array);


Output

Uint8Array(17) [
  4, 5, 4, 6, 5, 7, 6,
  8, 6, 5, 4, 3, 2, 3,
  4, 5, 6
]

Example 2: In this example, we will see the basic functionality of the Uint8Array array to create a new Unit8array performing the required transformations.

javascript




// Create a Uint8Array from a array by adding
// 3 to each number using function
let array = Uint8Array.from(
    [1, 2, 3, 4, 5, 6],
    (z) => z + 3
);
 
// Display the result
console.log(array);


Output

Uint8Array(6) [ 4, 5, 6, 7, 8, 9 ]
Learn to code easily with our course Coding for Everyone. This course is accessible and designed for everyone, even if you're new to coding. Start today and join millions on a journey to improve your skills!

Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Commit to GfG's Three-90 Challenge! Purchase a course, complete 90% in 90 days, and save 90% cost click here to explore.
Last Updated : 09 Jan, 2024
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads
Complete Tutorials