TypedArray.indexOf() function in JavaScript


The indexOf() function of the TypedArray object accepts a value and verifies whether the typed array contains the specified element. If so, this function returns the index of the array at which the specified element found, if the element occurred multiple timed this function returns the first index among them. If the array doesn’t contain the specified element the indexOf() function returns -1.

Syntax

Its Syntax is as follows

typedArray.indexOf(50)

Example

 Live Demo

<html>
<head>
   <title>JavaScript Array every Method</title>
</head>
<body>
   <script type="text/javascript">
      var int32View = new Int32Array([21, 19, 65, 21, 14, 66, 87, 55, 66, 97, 66 ]);
      document.write("Contents of the typed array: "+int32View);
      document.write("<br>");
      var contains = int32View.indexOf(66);
      document.write("Specified element occurred at the index: "+contains);
   </script>
</body>
</html>

Output

:
Contents of the typed array: 21,19,65,21,14,66,87,55
Specified element occurred at the index: 5

Example

 Live Demo

<html>
<head>
   <title>JavaScript Array every Method</title>
</head>
<body>
   <script type="text/javascript">
      var int32View = new Int32Array([21, 19, 65, 21, 14, 66, 87, 55, 66, 97, 66 ]);
      document.write("Contents of the typed array: "+int32View);
      document.write("<br>");
      var contains = int32View.indexOf(634);
      document.write("Specified element occurred at the index: "+contains);
   </script>
</body>
</html>

Output

:
Contents of the typed array: 21,19,65,21,14,66,87,55
Specified element occurred at the index: -1

Samual Sam
Samual Sam

Learning faster. Every day.

Updated on: 25-Jun-2020

28 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements