determine the type of the array as a boolean result

You can check the type of the first item of the array and assume that all items have the same type:

const numArray = [1,2,3,4];
const objArray = [{thing: 'value'}, {thing: 'value'}];

if (typeof numArray[0] === 'number') {
  console.log('This is an array of numbers.');
}

Or you can use Array.every() to check all of them:

if (objArray.every((item) => typeof item === 'object')) {
  console.log('This is an array of objects.');
}

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top