Predict the output of the following javascript code

console.log([10, 20, 30].indexOf(20));

 

 

 

 

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 from 0).

  • So, console.log([10, 20, 30].indexOf(20)); will output 1.