Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to check if an array contains integer values in JavaScript ?
In JavaScript, checking if an array contains integer values requires understanding the difference between numbers and strings that look like numbers. This article explores different approaches to detect actual integer values in arrays.
The Problem with String Numbers
Arrays often contain string representations of numbers like "123" instead of actual numbers. We need to distinguish between these types.
const mixedArray = ["123", 45, "hello", 67.5, "89"]; console.log(typeof "123"); // "string" console.log(typeof 45); // "number"
string number
Method 1: Using typeof and Number.isInteger()
The most reliable approach combines type checking with integer validation:
const findInteger = (arr = []) => {
const isInteger = num => {
return typeof num === 'number' && Number.isInteger(num);
};
return arr.some(isInteger);
};
const arr1 = ["123", "", "21345", "90"];
const arr2 = ["hello", 42, "world", 3.14];
const arr3 = [1, 2, 3];
console.log(findInteger(arr1)); // false - all strings
console.log(findInteger(arr2)); // true - contains 42
console.log(findInteger(arr3)); // true - all integers
false true true
Method 2: Including String Integers
If you want to detect both numeric integers and string representations of integers:
const hasIntegerValue = (arr = []) => {
return arr.some(element => {
// Check if it's a number and integer
if (typeof element === 'number') {
return Number.isInteger(element);
}
// Check if it's a string that represents an integer
if (typeof element === 'string') {
const num = Number(element);
return !isNaN(num) && Number.isInteger(num) && element.trim() !== '';
}
return false;
});
};
const testArray1 = ["123", "", "21345", "90"];
const testArray2 = ["hello", "3.14", "world"];
const testArray3 = [1.5, 2.7, "abc"];
console.log(hasIntegerValue(testArray1)); // true
console.log(hasIntegerValue(testArray2)); // false
console.log(hasIntegerValue(testArray3)); // false
true false false
Method 3: Using Array.find() for First Match
To find the first integer value instead of just checking existence:
const findFirstInteger = (arr = []) => {
const firstInteger = arr.find(element =>
typeof element === 'number' && Number.isInteger(element)
);
return firstInteger !== undefined;
};
const sampleArray = ["text", 3.14, 42, "123"];
console.log(findFirstInteger(sampleArray)); // true
true
Comparison
| Method | Detects Number Integers | Detects String Integers | Performance |
|---|---|---|---|
| typeof + Number.isInteger() | Yes | No | Fast |
| Combined Approach | Yes | Yes | Moderate |
| Array.find() | Yes | No | Fast (stops at first match) |
Key Points
-
Number.isInteger()only works with actual number types -
Array.some()returnstrueif any element matches -
Array.find()stops at the first match for better performance - String numbers like
"123"need separate validation
Conclusion
Use typeof combined with Number.isInteger() for strict integer checking. For broader validation including string integers, add string-to-number conversion with proper validation.
