22

I have arrays such as

var arrayVal_Int = ["21", "53", "92", "79"];   
var arrayVal_Alpha = ["John", "Christine", "Lucy"];  
var arrayVal_AlphaNumeric = ["CT504", "AP308", "NK675"];
  • Above arrayVal_Int should be considered as (purely) numeric.
  • arrayVal_Alpha and arrayVal_AlphaNumeric should be considered as strings.

I need to check that in JavaScript.

5
  • 2
    Any attempts to solve? Commented Sep 28, 2015 at 6:42
  • Welcome to SO. Please visit the help center to see how to ask questions but before you ask, please search for "test array numeric" here Commented Sep 28, 2015 at 6:43
  • An array contains more than a single value (usually). Do you want to know if the array contains exclusively numeric values? Commented Sep 28, 2015 at 6:44
  • try to loop over the array and apply parseInt function to see it returns ture. Commented Sep 28, 2015 at 6:44
  • @Touffy.yes.I need to check whether array is purely numeric Commented Sep 28, 2015 at 9:15

4 Answers 4

98

Shortest solution, evals to true if and only if every item is (coercible to) a number:

!yourArray.some(isNaN)
Sign up to request clarification or add additional context in comments.

5 Comments

@A.T. For browser support check out kangax.github.io/compat-table/es5/#Array.prototype.some (basically, works everywhere now)
Just learned this, except for empty string "" that will render as false on isNaN
@Mouser The empty string will coerce to zero so that is consistent with « coercible to a number ». You can switch isNaN for a more specific predicate if needed (and in many cases it won’t be needed).
This solution does not work if there is null
This solution does exactly what it says. Like the empty string, null coerces to zero. If you need null to make the code return false, you need to replace isNaN with something more specific.
10

I had a similar need but wanted to verify if a list contained only integers (i.e., no decimals). Based on the above answers here's a way to do that, which I posting in case anyone needs a similar check.

Thanks @Touffy, for your suggestion.

let x = [123, 234, 345];
let y = [123, 'invalid', 345];
let z = [123, 234.5, 345];

!x.some(i => !Number.isInteger(i))  // true
!y.some(i => !Number.isInteger(i))  // false
!z.some(i => !Number.isInteger(i))  // false

2 Comments

you could simplify that to x.every(Number.isInteger) instead of the double negation :)
BTW, I would have used every if there was a native isNumber function. Since isNaN is the opposite of isNumber and I wanted to use it without creating an extra function isNumber = n => !isNaN(n), I exploited the fact that ∀𝑛|¬𝜆(𝑛) is logically equivalent to ∄𝑛|𝜆(𝑛). But you don't have to do that with isInteger.
2

Using simple JavaScript, you can do something like this:

var IsNumericString = ["21","53","92","79"].filter(function(i){
    return isNaN(i);
}).length > 0;

It will return true;

Comments

0

Try this:

let x = [1,3,46,7,7,8];
let y = [1,354,"fg",4];
let z = [1, 3, 4, 5, "3"];

isNaN(x.join("")) // false
isNaN(y.join("")) // true
isNaN(z.join("")) // false

1 Comment

This doesn't work. There are sequences with non-numeric strings that can join into a valid numeric string (e.g. "1", "e5"), and vice-versa ("1.2", ".3").

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.