How to use JavaScript Array findIndex()

Program to use JavaScript Array findIndex() method


let a = [15, 9, 2, 13, 24, 21];

let b = a.findIndex(function (c) {
    return c > 18;
});

console.log(b);

Output


4

Steps Description

  1. Declare three variables a, b and c
  2. Initialize three variables a, b and c with value of string a = [15, 9, 2, 13, 24, 21], b = a.findIndex(function (c) and c > 18
  3. Array findIndex() method finds the given statement in array element if any statement element matches then prints its index number
  4. The value of b variable is displayed with the help of console.log inbuilt function