javascript check if item already exists in array code example

Example 1: javascript method to see if item exists in array

var fruits = ["Apple", "Banana", "Mango", "Orange", "Papaya"];
    
    // Check if a value exists in the fruits array
    if(fruits.indexOf("Mango") !== -1){
        alert("Value exists!")
    } else{
        alert("Value does not exists!")
    }

Example 2: check if an element is already in an array

private boolean ZitAlInArray(int value, List<Integer> list) {
  return array.indexOf(value) > -1;
}

private boolean zitAlInArray(int[] array, int index) {
		for (int i = 0; i < index; i++) {
			if (array[i] == array[index]) {
				return true;
			}
		} 
		return false;
	}