constarr= [1,2,3]// Method 1Array.isArray(arr) // true// Method 2console.log(arr instanceofArray) // true // but also note - // arr instanceof Object is also true, so you can do this belowif(Array.isArray(arr) && arr instanceofArray) // actually Array.isArray is sufficient
Test if a variable is an Object
// This is tricky because array is an object, function is an object so it's difficult// to find the actual object// Problemconstarr= []constfun=function(){}constactualObj= {}arr instanceofObject// truefun instanceofObject// trueactualObj instanceofObject// true// So the goal is actualObj must return true and everything else falseactualObj instanceofObject&&!(actualObj instanceofArray) &&!(typeof actualObj ==='function')