Saturday, December 28, 2024
Google search engine
HomeLanguagesJavascriptJavaScript typedArray.indexOf() Method

JavaScript typedArray.indexOf() Method

The typedArray.indexOf() is an inbuilt function in JavaScript which is used to return the index of the element if found in the given typedArray otherwise it returns -1. 

Syntax:

typedarray.indexOf(Element, Index);

Parameters: It accepts two parameter which are specified below-

  • Element: It is the element which are being searched for index in the typedArray.
  • Index: It is the index of the element in the typedArray form where searching should be started. Its default value is zero (0) and it is optional.

Return value: It returns the index of the element if found in the given typedArray otherwise it returns -1.

JavaScript code to show the working of this function:

Example 1: 

javascript




// Creating some typedArrays
const A = new Uint8Array([ 1, 2, 3, 4, 5 ]);
const B = new Uint8Array([ 5, 10, 15, 20 ]);
const C = new Uint8Array([ 0, 2, 4, 6,, 8, 10 ]);
const D = new Uint8Array([ 1, 3, 5, 7, 9 ]);
  
// Calling indexOf() function
a = A.indexOf(2)
b = B.indexOf(15, 1)
c = C.indexOf(6)
d = D.indexOf(9, 1)
  
// Printing the index of the elements given
// as the parameter of the indexOf() function
console.log(a);
console.log(b);
console.log(c);
console.log(d);


Output:

1
2
3
4

Example 2: 

javascript




// Creating some typedArrays
const A = new Uint8Array([ 1, 2, 3, 4, 5 ]);
const B = new Uint8Array([ 5, 10, 15, 20 ]);
const C = new Uint8Array([ 0, 2, 4, 6,, 8, 10 ]);
const D = new Uint8Array([ 1, 3, 5, 7, 9 ]);
  
// Calling include() function
a = A.indexOf(6)
b = B.indexOf(21, 1)
c = C.indexOf(6, 4)
d = D.indexOf(0)
  
// Printing the index of the elements given
// as the parameter of the indexOf() function
console.log(a);
console.log(b);
console.log(c);
console.log(d);


Output:

-1
-1
-1
-1

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!

Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments