1// inside if else check
2if(Array.isArray(myVarToTest)) {
3 // myVatToTest is an array
4} else {
5 // myVarToTest is not an array
6}
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
2function isArray(value) {
3 return Object.prototype.toString.call(value) === "[object Array]";
4}