1var colors=["red","green","blue"];
2
3if(Array.isArray(colors)){
4 //colors is an array
5}
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// Check if something is an Array
2// just like you do with "typeof"
3Array.isArray([1, 2, 3]); // true
4Array.isArray('asdf'); // false
1var extensions = ["image/jpeg","image/png","image/gif"];
2if(extensions.indexOf("myfiletype") === -1){
3 alert("Image must be .png, .jpg or .gif");
4}