Last Updated : Jul 1, 2023
Here we defined an array ‘arr’ with numeric values ‘[20,30,7,4,89]’ then we used findIndex method on that so we can find index of particular value from array ‘arr’.
Within findIndex method we need to give some condition for find that particular value so we checks which value from array is equal to ‘89’ then it will return its position from array.
We can also use greatest value from an array condition result will be same.
<!DOCTYPE html> <html> <head> <title>FIND INDEX IN ARRAY JS</title> </head> <body> <script> const arr=[20,30,7,4,89]; const ind=arr.findIndex(arrval=>arrval === 89); console.log("Array Index Value Is",ind); </script> </body> </html>
- <!DOCTYPE html> tag which is instruct the web browser about what version of HTML file written in and it’s not have any ending tag.
- The<html> tag is used to indicate the beginning of HTML document.
- As above shown <head> tag is contains information about webpage and external file links are declared here. <title> tag is used for set the webpage title.
- Both <head> and <title> tags having their pair end tag, so we need to close the ending tags respectively. If you’re not closed anyone of ending tag properly that is also affect the webpage result.
- <body> tag is beginning of main coding part because it contain coding of entire website blocks and elements described here.
- Here we defined an array variable ‘arr’ with values ‘[20,30,7,4,89]’ then we defined another variable ‘ind’ for store index of found value from array.
- Within findIndex method we need to use some variable for iterate purpose so we defined ‘arrval’ variable then this method needs to appends with array ‘arr’.
- Then ‘arrval’ will iterate each value in array with condition equal to value ‘89’ which is matched so we finally get result ‘4’ and displayed on console.
- Both </body>,</html> tags closed respectively. </body> tag indicates the end of body, Then </html> tag indicates the end of HTML document.
When we executes our program on browser we can’t see anything because our result is printed on console panel.
For saw result we need to use shortcut ‘ctrl+shift+j’ then console panel will open with result ‘Array Index Value Is 4’.
When we using ‘console.log()’ method for display we need to open console panel otherwise we can’t see result.