Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

One more effective method to verify the type of an Array containing elements of any type is to use the Array.isArray() method along with the Array.every() method. This method checks if the array is an array and if every element of the array satisfies a certain condition. For example, to verify if every element in the array is of type 'string':

let array = [1, 'hello', true];

if (Array.isArray(array) && array.every((element) => typeof element === 'string')) {
  console.log('Array is of type string');
} else {
  console.log('Array is not of type string');
}

This method checks if the array is an array and if every element in the array is of type 'string'. If every element satisfies the condition, it will return true, otherwise it will return false.