Computer >> Computer tutorials >  >> Programming >> Javascript

TypedArray.fill() function in JavaScript


The fill() function of the TypedArray object replaces all the required/desired elements of the array with specifies value (fixed). This function accepts three numbers one representing a fixed value and the other two represents the start and end indexes of the portion of the elements to be replaced (end value is optional).

Syntax

Its Syntax is as follows

int32View.fill(464, 3);

Example

<html>
<head>
   <title>JavaScript Array every Method</title>
</head>
<body>
   <script type="text/javascript">
      var int32View = new Int32Array([64, 89, 65,21, 14, 66, 87, 55]);
      document.write("Contents of the typed array: "+int32View);
      document.write("<br>");
      result = int32View.fill(464, 3);
      document.write("Contents of the typed array after fill: "+int32View);
   </script>
</body>
</html>

Output

Contents of the typed array: 64,89,65,21,14,66,87,55
Contents of the typed array after fill: 64,89,65,464,464,464,464,464