javascript check if number in array code example

Example 1: get if there is a value in an array node js

myArray = Array(/*element1, element2, etc...*/);

// If the array 'myArray' contains the element 'valueWeSearch'
if(myArray.includes(valueWeSearch))
{
 	// Do something
}

Example 2: check if array does not contain value javascript

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

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

var n = fruits.includes("Django"); // false

Example 3: javascript array contains

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

Example 4: javascript check if in array

var extensions = ["image/jpeg","image/png","image/gif"];          
if(extensions.indexOf("myfiletype") === -1){
	alert("Image must be .png, .jpg or .gif");
}

Tags:

Php Example