Sunday, December 29, 2024
Google search engine
HomeLanguagesJavascriptJavaScript Unit16Array.from() Method

JavaScript Unit16Array.from() Method

The Javascript Uint16Array represents an array of 16-bit unsigned integers in the platform byte order. By default, the contents of Uint16Array are initialized to 0. The Uint16Array.from() function is used to create a new Uint16Array from an array-like or iterable object. So when you want to convert an arrayLike or iterable object then you can use this function by passing the object as a parameter to this function along with the map function and value used for the map function if needed. 

Syntax:

Uint16Array.from( source, mapFn, thisArg )

Parameters: This method accepts three parameters as mentioned above and described below.

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

Return Value: This method returns a new Uint16Array instance. 

The below examples illustrate the Unit16Array.from() Method in JavaScript: 

Example 1: This example shows the basic working of the Unit16Array.from() Method in JavaScript.

javascript




// Create a Uint16Array from a string
// like structure
let array = Uint16Array.from('543234543');
 
// Print the result
console.log(array);


Output:

5, 4, 3, 2, 3, 4, 5, 4, 3

Example 2: This example shows the basic working of the Unit16Array.from() Method in JavaScript.

javascript




// Create a Uint16Array from a array
// multiplying 3 to each number
// using function
let array = Uint16Array.from([32,
    53, 122, 434, 213], z => z * 3);
 
// Print the result
console.log(array);


Output:

96, 159, 366, 1302, 639
Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen 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!

RELATED ARTICLES

Most Popular

Recent Comments