How to use JavaScript Array lastIndexOf()

Array lastIndexOf()

Array lastIndexOf() method

Program to use JavaScript Array lastIndexOf() method

let a = ["Html", "Css", "JavaScript","Html","Css"];
    let b = a.lastIndexOf("Html");
    console.log(b);
    

Output

3
    

Steps Description

  • Declare two variable a and b
  • Initialize two variable with value of array a = [“Html”, “Css”, “JavaScript”,”Html”,”Css”] and b = a.lastIndexOf(“Html”)
  • Array lastIndexOf() method returns the last index of the given element
  • The value of b variable is displayed with the help of console.log inbuilt function
  • Array lastIndexOf() method

    Program to use JavaScript Array lastIndexOf() method

    let c = ["Html", "Css", "JavaScript","Html","Css","JavaScript"];
        let d = c.lastIndexOf("Css");
        console.log(d);
        

    Output

    4
        

    Steps Description

    • Declare two variable c and d
    • Initialize two variable with value of array c = [“Html”, “Css”, “JavaScript”,”Html”,”Css”,”JavaScript”] and d = c.lastIndexOf(“Css”)
    • Array lastIndexOf() method returns the last index of the given element
    • The value of d variable is displayed with the help of console.log inbuilt function