check if variable is array or object javascript code example

Example 1: if object is array javascript

Array.isArray(object);

Example 2: javascript check if is array

var colors=["red","green","blue"];

if(Array.isArray(colors)){
    //colors is an array
}

Example 3: javascript is array or object

const 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

Tags:

Misc Example