TypedArray set() JavaScript

The Javascript TypedArray set() method holds values into an array.

Syntax:

array.set(array, Index(Offset)) 

Parameters:
array: It represents source array.
Index(Offset): It represents the index position of the source array from where to start. Default value is 0.

Returns:
Modified array.

Example 1:

<!DOCTYPE html>
<html>
<body>
<script type="text/javascript">   
var a = new ArrayBuffer(8);   
var b = new Uint8Array(a);  
b.set([20,50,50,40,50],1);  
document.write(b);  
</script>
</body>
</html>

Output:

0,20,50,50,40,50,0,0

Example 2:

<!DOCTYPE html>
<html>
<body>
<script type="text/javascript"> 
const testBuffer = new ArrayBuffer(8);
const testArray = new Uint8Array(testBuffer);
 
testArray.set([13, 22, 43], 2);
 
document.write(testArray);
 
</script>
</body>
</html>

Output:

0,0,13,22,43,0,0,0
Please Share