JavaScript TypedArray.of() Method
The TypedArray.of() method is used to create a new array from a variable number of arguments that are passed to it.
Syntax:
TypedArray.of( el0, el1, el2, ...elN )
Parameters: This method accepts a variable number of elements, which are used to create the Int16Array array.
TypedArray can contain any of the below-mentioned values:
Int8Array
Uint8Array
Uint8ClampedArray
Int16Array
Uint16Array
Int32Array
Uint32Array
Float32Array
Float64Array
BigInt64Array
BigUint64Array
Return Value: This method returns a new TypedArray instance.
The below example illustrates the TypedArray.of() Method in JavaScript:
Example 1: In this example, the values passed are the character values that are converted to Int16 by the method.
JavaScript
<script>
let int16Arr = new Int16Array;
int16Arr =
Int16Array.of( '31' , '52' , '16' , '80' , '24' );
console.log(int16Arr);
</script>
|
Output:
[31, 52, 16, 80, 24]
Example 2: In this example, the values passed are the Integer values which are converted to Int16 by the method. The value -499999 is converted to 15537.
JavaScript
<script>
let int16Arr = new Int16Array;
int16Arr =
Int16Array.of(-49999, 5, 7, 799, 8);
console.log(int16Arr);
</script>
|
Output:
[15537, 5, 7, 799, 8]
Example 3: In this example, the values passed are the character values that are converted to Uint16 by the method.
JavaScript
<script>
let uint16Arr = new Uint16Array;
uint16Arr =
Uint16Array.of( '31' , '50' , '26' , '60' , '24' );
console.log(uint16Arr);
</script>
|
Output:
[31, 50, 26, 60, 24]
Example 4:In this example, the values passed are the Integer values which are converted to Uint16 by the method. The value -499999 is thus converted to 24289.
JavaScript
<script>
let uint16Arr = new Uint16Array;
uint16Arr =
Uint16Array.of(-499999, 5, 7, 799, 8);
console.log(uint16Arr);
</script>
|
Output:
[24289, 5, 7, 799, 8]
Supported Browsers:
- Chrome
- Edge
- Firefox
- Opera
- Safari
Last Updated :
10 Jan, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...