JavaScript | Int8Array from() Method

The Int8Array array represents an array of twos-complement of 8-bit signed integers. By default, the contents of Int8Array are initialized to 0. from the () function of Int8Array used to creates a new Int8Array from an array-like or iterable object. So when you want to convert an arrayLike or iterable object to Int8Array then you can use this function by passing the object as a parameter to this function along with map function and value used for map function if needed.

Synatx:

Int8Array.from(source[, mapFn[, thisArg]])

parameters: This method accepts three parameters that are specified below-

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

Return Value: this method returns a new Int8Array instance.

JavaScript program to Illustrate the working of from() function:
Program 1:



filter_none

edit
close

play_arrow

link
brightness_4
code

<script>
    //create a Int8Array from a string like structure
    var array = Int8Array.from('876543456789');
  
    //print the result
    document.write(array);
</script>

chevron_right


Output:

8, 7, 6, 5, 4, 3, 4, 5, 6, 7, 8, 9

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

<script>
    //create a Int8Array from a array by 
    //adding 1 to each number using function
    var array = Int8Array.from([9, 2, 1, 4, 3], z => z + 1);
  
    //print the result
    document.write(array);
</script>

chevron_right


Output:

10, 3, 2, 5, 4

References:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray/from#

full-stack-img

My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.