Universal isNum() function in JavaScript
Implementation of a fast and universal isNum() check if a given number or string contains only a number.
Motivation
For my project I needed a function to check if a given argument is a number or a string contains only a number, without units.
There are a lot of methods to check a string in Javascript to be a number. But none of them are handling the edge cases properly. Often empty strings are identified as numbers.
Here is my implementation:
It return false if there are any other characters than digits or the number is misspelled.
- The number can contain one point or comma.
- At the begin or end can be unlimited spaces in the string.
- For empty strings, only spaces, only points or commas or any combination of these
falseis returned.
/** ### Is given string a number?
* - can check numbers or strings
* - the number can contain one point or comma
* - the string can contain spaces
* - only space(s) or point(s)/comma(s) are not numbers
*
* @overview
*
* Converts a number into a string and \
* iterates over all characters of the string.
*
* @param checkThis - number or string
* @returns boolean
* - true if argument is a "pure" number (int or float (without E notation))
* - false otherwise
* */
const isNum = (checkThis: any):boolean => {
let digit = false // digit(s) was found,
let gap = false // flag of gap check
let point = false //
/** check every char. If non digit, space or point found: exit */
for (const char of `${checkThis}`) { // fast way to ensure we have a string to check
let isSep = ".,".includes(char)
if (char === " ") { gap = digit; continue } // space => set flag for gap check and next loop iteration
if (isSep && point) { digit = false; break } // second point found => no number = exit function
if (isSep) { point = true; continue } // point found => set flag for check for more than one point
let isNum = "0123456789".includes(char)
if (! isNum) { digit = false; break } // no digit => no number => exit function
if (isNum && gap) { digit = false; break } // digit after space(s) (=gap) => no number = exit function
digit = true // digit found -> number
}
return digit
}Check only numbers and strings
With the provided code you can check any type. For boolean, objects and arrays, etc. the return value is false, since their toString() function is called.
Get Thomas Schroeder’s stories in your inbox
Join Medium for free to get updates from this writer.
To check only for strings and numbers, change the typescript parameter type to string | number.
(checkThis: any) => (checkThis: string | number)How it works
- Converts a number argument into a string:
`${number}`is the (2nd) fastest way to do it. - Iterates over all characters of the (given) string.
- If a space is found, it will be marked (see below) and next char is retrieved.
- If a second point or comma is found (see below), exit the function with
false. - If a point or comma is found, it will be marked and next char is retrieved.
- Check if the character is a digit
- if it is not, exit the function with
false. - if there was/were digit(s) and another space is found, exit the function with
false. - If no exit happened and we checked all characters, we have a number and
trueis returned. Yeah!
Limitations
- No scientific or exponential notation
I did ignore to check numbers with scientific or exponential notation. Was not needed for this project and probably in 99,9% of all use cases of aisNum()function. - The function does not throw errors to inform the developer of misspelled or misformated arguments. It “just checks what is was fed to”.






