js check if array or object code example

Example 1: if object is array javascript

Array.isArray(object);

Example 2: in javascript check is is an array or not

Array.isArray(yourItem)

Example 3: how to check if something is array javascript

function isArray(value) {
    return Object.prototype.toString.call(value) === "[object Array]";
}

Example 4: how to check if array

// Check if something is an Array 
// just like you do with "typeof"
Array.isArray([1, 2, 3]);	// true
Array.isArray('asdf');		// false

Example 5: check object in array javascript

var obj = {a: 5};
var array = [obj, "string", 5]; // Must be same object
array.indexOf(obj) !== -1 // True

Example 6: 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