javascript if in array code example

Example 1: javascript array contains

var colors = ["red", "blue", "green"];
var hasRed = colors.includes("red"); //true
var hasYellow = colors.includes("yellow"); //false

Example 2: typescript check if element in array

your_array.includes(the_element)

Example 3: js test if array

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

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: how to check if item is in list js

var myList=["a", "b", "c"];
mylist.includes("d")//returns true or false

Example 6: javascript array contains

var fruits = ["Banana", "Orange", "Apple", "Mango"];
var n = fruits.includes("Mango");

Tags:

Php Example