javascript inarray check code example

Example 1: javascript element in array

var fruits = ["Banana", "Orange", "Apple", "Mango"];

var n = fruits.includes("Mango");

Example 2: js test if array

if(Array.isArray(myVarToTest)) {
	// myVatToTest is an array
} else {
	// myVarToTest is not an array
}

Example 3: js check if array

Array.isArray([1, 2, 3]);	// true
Array.isArray('asdf');		// false

Example 4: javascript is int in array

[1, 2, 3].includes(2);     // true
[1, 2, 3].includes(4);     // false
[1, 2, 3].includes(1, 2);  // false (second parameter is the index position in this array at which to begin searching)

Example 5: check items in array javascript

function checkAllEven(arr) {
  return arr.every(function(x){
	 return	x % 2 === 0
	})
}

//using "every" to check every item in array.