Quirrel4.41.0

types.Array.indexof

Method on every array
pure array.indexof(value): any

Finds the index of the first element that equals value.

The binding carries no declaration string, so the VM cannot report parameter names. The names above are from this page; the types and attributes still come from the VM. The VM also cannot tell an optional parameter from a variadic tail here, so read the brackets and any trailing ... from the prose below, not from the signature.

Parameters

valueanythe value to look for

Return value

The index of the first match, or null if none matched.

Notes

Equality is the same raw comparison contains uses: identity for tables, arrays, classes and instances, value for numbers and strings, and no _cmp or _eq metamethod. Use findindex when the match is a condition rather than a fixed value.

Example

examples/types/array/indexof.nut
let a = ["a", "b", "c"];
println("a.indexof(\"b\") =", a.indexof("b"));
println("a.indexof(\"z\") =", a.indexof("z"));

let t = {};
println("[t, {}].indexof(t) =", [t, {}].indexof(t));   // reference identity, not structural equality
Output:
a.indexof("b") = 1
a.indexof("z") = null
[t, {}].indexof(t) = 0

See also

containsReports whether any element equals value.
findindexFinds the index of the first element for which callback returns a true value.
arrayclass index