Predict the output of the following javascript code
console.log([10, 20, 30].indexOf(20));
Quiz Explanation
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.
In this case, you're calling indexOf(20)
on the array [10, 20, 30]
.
The element
20
is present in the array.Its index is
1
(remember, array indices start from0
).So,
console.log([10, 20, 30].indexOf(20));
will output1
.