privacylong.blogg.se

Indexof javascript
Indexof javascript












Indexof javascript code#

Onclick of the button " Find" in the HTML code fires the function myPosition() in the block, at the same time indexOf() method search for an element " India" in an array and return the position of a element as output.ĮXAMPLE: Starting for the position of " India" in an array, starting the search position at 3. We need to return the position of a specified item (" India") by searching in an array, for that we are using indexOf() method. In the above code snippet we have given Id as " myId "to the second element in the HTML code. There is a function myPosition() in the block which is connected to the onclick of the HTML button and array with value "" to the variable a. Var a = ĭocument.getElementById(" myId").innerHTML = b In JavaScript, by using indexOf() method we can search and return the position of a specified element.ĮXAMPLE: Searching for the position of " India" in an array Click the below button to know the position of India. NOTE: The position for the first item is ' 0', the position for the second item is ' 1', like that it continues and so on. In case if the specified item is not found in the search, the Array indexOf returns ' -1' as output. If we give both ' item' and ' start', the serach will start from the specified position in ' start'.

indexof javascript indexof javascript

If we give only ' item' the search will start from the beginning of an array and returns the specified item position. The parameters for Array indexOf is ' item' and ' start'.

indexof javascript

The Array indexOf searches in the array for a specified element and return it's position. In previous posts, we learnt about Sorting, Adding, Slicing, Reversing, Joining array in JavaScript.












Indexof javascript