Quirrel4.41.0

types.Array.contains

Method on every array
array.contains(value): any

Reports whether any element 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

true if some element equals value, false otherwise.

Notes

Equality is the same raw comparison == uses: a number compares by value against another number, and anything else compares by identity, so two distinct tables or arrays with the same contents are never equal here. No _cmp or _eq metamethod is consulted.

hasvalue is this same function registered under a second name.

Example

examples/types/array/contains.nut
let a = [1, 2, 3];
println("a.contains(2) =", a.contains(2));
println("a.contains(9) =", a.contains(9));

// equality is by reference for tables/instances, not by contents
let t = {x = 1};
println("[t].contains(t) =", [t].contains(t));         // same table
println("[{x = 1}].contains(t) =", [{x = 1}].contains(t));   // an equal-looking but different table
Output:
a.contains(2) = true
a.contains(9) = false
[t].contains(t) = true
[{x = 1}].contains(t) = false

See also

hasvalueReports whether any element equals value.
indexofFinds the index of the first element that equals value.
arrayclass index