Partager via


Array.contains Function

Determines whether the specified object exists as an element in an Array object. This function is static and can be invoked without creating an instance of the object.

var itemExists = Array.contains(array, item);

Arguments

Term

Definition

array

The array to search.

item

The object to find in the array.

Return Value

true if the specified object exists as an element in the array; otherwise, false.

Remarks

Use the contains function to determine whether a specified object exists as element in an Array object.

In Mozilla Firefox, calling the contains function with item set to undefined returns true if an item in the array has been set to undefined. In all other browsers, the function returns false in these circumstances.

Example

The following example shows how to use the contains function to determine whether an array contains a specified element.

var a = ['red', 'green', 'blue', 'yellow'];
var b = Array.contains(a, "red");
// View the results: "true"
alert(b.toString());

See Also

Reference

Array Object

Array Type Extensions

Other Resources

Language Reference