How to use IndexOf in JavaScript

In this article I am going to explain about IndexOf() Method in JavaScript.
  • 3509

JavaScript IndexOf() Method

JavaScript IndexOf() Method use for search  specifics value index number from the array . Search will be start from specifics given position if position does not set then search will start from beginning. Beginning position start from 0. After search it  will return index number of values if does not match it will return -1. IndexOf() method does not work in Internet explorer 8 and earlier.

Syntax

string.indexOf(searchvalue, startposition)

Example

In this we will find the index number

<html>

<head>

    <script type="text/javascript">

        function myfun() {

            var name = ["Sanjay", "Ajay", "Deepu", "Arun"];

            var st = name.indexOf("Ajay")

            var x = document.getElementById("test");

            x.innerHTML = st;

        }

</script>

</head>

<body>

    <p id="test">

        click for display index no</p>

    <button onclick="myfun()">

        click me</button>

</body>

</html>

Output

indexof.jpg

Further Readings

You may also want to read these related articles: here
Ask Your Question 
 
Got a programming related question? You may want to post your question here
 

Categories

More Articles

© 2019 DotNetHeaven. All rights reserved.