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