Skip to content
Related Articles

Related Articles

Improve Article

JavaScript | Int8Array from() Method

  • Last Updated : 23 Jul, 2021

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.
Syntax: 
 

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: 
 

javascript




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

Output:  

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

Program 2: 
 

javascript




<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>

Output:  

10, 3, 2, 5, 4

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

My Personal Notes arrow_drop_up
Recommended Articles
Page :