1var colors = ["red", "blue", "green"];
2var hasRed = colors.includes("red"); //true
3var hasYellow = colors.includes("yellow"); //false
1if(Array.isArray(myVarToTest)) {
2 // myVatToTest is an array
3} else {
4 // myVarToTest is not an array
5}
1// If you want to test if a variable (object) is an instance of
2// an array. You can use the Array.isArray(varName) method
3// this method takes one argument, the variable or object you want to test
4// and returns true, if the argument is an array
5// or false, if the argument is not an array
6
7if (Array.isArray(object)) {
8 // is true
9} else {
10 // is false
11}
12
13Array.isArray([1, 2, 3, 4]); // Returns true
14Array.isArray(4); // returns false
15// this could be used for flattening an array of arrays
16
1
2var fruits = ["Banana", "Orange", "Apple", "Mango"];
3var n = fruits.includes("Mango");
4
1var extensions = ["image/jpeg","image/png","image/gif"];
2if(extensions.indexOf("myfiletype") === -1){
3 alert("Image must be .png, .jpg or .gif");
4}