How to check if an array contains integer values in JavaScript ?


We are required to write a JavaScript function that takes in an array of elements.

Our function should check whether or not the array contains an integer value.

We should return true if it does false otherwise.

Example

The code for this will be −

const arr = ["123", "", "21345", "90"];
const findInteger = (arr = []) => {
   const isInteger = num => {
      return typeof num === 'number';
   };
   const el = arr.find(isInteger);
   return !!el;
};
console.log(findInteger(arr));

Output

And the output in the console will be −

false

Updated on: 24-Nov-2020

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements