Let us try one example
var scripts = new Array();
scripts = "PHP";
scripts = "ASP";
scripts = "HTML";
The output is 1. The element ASP is present at two places, starting from beginning it is at 1st position ( PHP is 0 position ) .
We can search and if found get the position of the element inside Array by using lastIndexOf function. The difference between indexOf and lastIndexOf is searching the array starts from end in later case.
Here is an example.
The output of above code is 4 as ASP is there in 4th position ( PHP is 0 position). Note that lastIndexOf() starts searching from the end of the array so it got the search string at 4th position. Same way if we use indexOf function we would have got 1 as output.
← Array ReferenceSort the elements of an Array →