Last Updated : 15 Jul, 2025
Here are the various methods to validate numbers in JavaScript
1. Check if a Value is a NumberUse typeof and isNaN() to determine if a value is a valid number.
JavaScript
const isNum = n =>
typeof n === 'number' && !isNaN(n);
console.log(isNum(42));
console.log(isNum('42'));
console.log(isNum(NaN));
In this example
Use regular expressions or isFinite() to validate numeric strings.
JavaScript
const isNumStr = s =>
!isNaN(s) && isFinite(s);
console.log(isNumStr('123'));
console.log(isNumStr('123abc'));
console.log(isNumStr('1.23'));
In this example
Check if a number is an integer using Number.isInteger().
JavaScript
const isInt = n => Number.isInteger(n);
console.log(isInt(42));
console.log(isInt(3.14));
console.log(isInt('42'));
In this example
Use a combination of checks to ensure a number is a float.
JavaScript
const isFloat = n =>
typeof n === 'number' && !Number.isInteger(n);
console.log(isFloat(3.14));
console.log(isFloat(42));
console.log(isFloat('3.14'));
In this example
Ensure a number falls within a specific range.
JavaScript
const inRange = (n, min, max) =>
n >= min && n <= max;
console.log(inRange(10, 5, 15));
console.log(inRange(20, 5, 15));
console.log(inRange(5, 5, 15));
In this example
Use regular expressions for specific number formats.
JavaScript
const isStrict = s => /^-?\d+(\.\d+)?$/.test(s);
console.log(isStrict('123'));
console.log(isStrict('-123.45'));
console.log(isStrict('abc123'));
In this example
Check if a number is positive.
JavaScript
const isPos = n =>
typeof n === 'number' && n > 0;
console.log(isPos(42));
console.log(isPos(-42));
console.log(isPos(0));
In this example
Check if a number is negative.
JavaScript
const isNeg = n =>
typeof n === 'number' && n < 0;
console.log(isNeg(-42));
console.log(isNeg(42));
console.log(isNeg(0));
In this example
Determine if a number is even.
JavaScript
const isEven = n =>
typeof n === 'number' && n % 2 === 0;
console.log(isEven(4));
console.log(isEven(7));
console.log(isEven(-2));
In this example
Check if a number is odd.
JavaScript
const isOdd = n =>
typeof n === 'number' && n % 2 !== 0;
console.log(isOdd(7));
console.log(isOdd(4));
console.log(isOdd(-3));
In this example
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4