You are misunderstanding the notation.
ofstring.indexOf(searchValue[, fromIndex])
This means that the second argument to this function, fromIndex
, is optional. If provided, it tells indexOf
where to start looking. Otherwise, it is defaulted to 0
.
To call the two-argument form, the syntax is:
ofstring.indexOf(value, index)
and note in particular that there are no square brackets. The square brackets are just a notational convenience for the documentation.
As for why the square brackets are there in your code, note that sA
in the below code is an index. So to obtain the value of secondOnArray
at index sA
, we need to index it, hence the square brackets. This is unrelated to the two-argument form of indexOf
.
for (var sA in secondOnArray) {
if (firstOnArray.indexOf(secondOnArray[sA]) > -1) {
result += 0;
}
}
One might consider writing instead
secondOnArray.forEach(function (ele) {
if (firstOnArray.indexOf(ele) > -1) {
result += 0;
}
});
which specifies the intention clearer.