The indexOf()
method in JavaScript returns the first index at which a given element can be found in the array, or -1 if it is not present.
Here’s an example:
const numbers = [1, 2, 3, 4, 5];
const index = numbers.indexOf(3);
console.log(index);
This code will output: 2, which is the index of the number 3 in the numbers
array. If the given element is not present in the array, the method will return -1
.
For example:
const numbers = [1, 2, 3, 4, 5];
const index = numbers.indexOf(6);
console.log(index);
This code will output: -1
, indicating that the number 6 is not present in the numbers
array.
+ There are no comments
Add yours