We can check if a certain element exists in an array by using the contains()
method.
arr.contains(elem)
arr
: This is the array we want to check to see if it contains a certain element.
elem: This is the element we want to check to see if it exists in the array arr
.
A Boolean value is returned. true
is returned if the element elem
exists in array arr
. Otherwise, it returns false
.
// create arrays let gnaam = ["Google", "Netflix", "Amazon", "Apple", "Meta"] let vowels = ["a", "e", "i", "o", "u"] let languages = ["C", "Swift", "Java"] // check if some elements are present print(gnaam.contains("Facebbok")); // false print(gnaam.contains("Meta")) // true print(vowels.contains("a")) // true print(vowels.contains("z")) // false print(languages.contains("Python")) // false print(languages.contains("C")) // true
gnaam
, vowels
, and languages
.contains()
method on the arrays we created, and pass some parameters to it. We then print the results on the console.RELATED TAGS
CONTRIBUTOR
View all Courses