Open In App

JavaScript Int8Array from() Method

Last Updated : 26 May, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

The Javascript Int8Array 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 create 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 the map function and value used for the map function if needed.

Syntax:  

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

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

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

Return Value: This method returns a new Int8Array instance.

JavaScript examples to Illustrate the working of from() function: 

Example 1: In this example, we will see the use of  JavaScript Int8Array from() Method.

javascript




//create a Int8Array from a string like structure
let array = Int8Array.from('876543456789');
 
//print the result
console.log(array);


Output:  

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

Example 2: In this example, we will add one to every element of the array using the JavaScript Int8Array from() Method. 

javascript




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


Output:  

10, 3, 2, 5, 4

We have a complete list of Javascript Array methods, to check those please go through this Javascript Array Complete reference article.

Supported Browsers: The browsers supported by JavaScript Array isArray() method are listed below:

  • Google Chrome 5.0
  • Microsoft Edge 12
  • Mozilla Firefox 4.0
  • Safari 5.0
  • Opera 10.5

We have a Cheat Sheet on Javascript where we covered all the important topics of Javascript to check those please go through Javascript Cheat Sheet-A Basic guide to JavaScript


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads