1var colors=["red","green","blue"];
2
3if(Array.isArray(colors)){
4 //colors is 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
1const data = [1,2,3,4,5];const isArray = Object.prototype.toString.call(data) === '[object Array]';console.log(isArray); // trueconst data = {id: 1, name: “Josh”};const isArray = Object.prototype.toString.call(data) === '[object Array]';console.log(isArray); // false
1To remember the difference, you can think of "is a / has a"
2
3let fruits = [
4 orange,
5 mango,
6 banana
7];
8// Every item in the array "fruits" is a fruit.
9
10let fruit = {
11 seed:{},
12 endocarp:{},
13 flesh:{}
14};
15// The object fruit has every attribute inside the object.