Number Validation in JavaScript
Last Updated :
03 Jan, 2025
Here are the various methods to validate numbers in JavaScript
1. Check if a Value is a Number
Use 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
- typeof n === 'number' checks if the type of n is a number.
- !isNaN(n) ensures the value is not NaN (Not a Number).
- It ensures both type and value validity.
2. Check if a String is Numeric
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
- !isNaN(s) ensures the string can be parsed as a number.
- isFinite(s) ensures the value is not infinite.
- This works for integers and decimals.
3. Validate Integer
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
- Number.isInteger(n) directly validates if n is an integer.
- It doesn't convert strings or other types to numbers.
- The function is simple and precise.
4. Validate Floating-Point Numbers
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
- typeof n === 'number' ensures the value is a number.
- !Number.isInteger(n) confirms it's not an integer.
- This validates numbers with decimal points.
5. Range Validation
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
- The function checks if n is greater than or equal to min and less than or equal to max.
- It’s a quick way to validate ranges.
6. Regex for Strict Validation
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
- The regex ^-?\d+(\.\d+)?$ checks for optional negative signs, digits, and decimals.
- This ensures strict numeric validation for strings.
7. Validate Positive Numbers
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
- typeof n === 'number' ensures the input is a number.
- n > 0 validates that the number is positive.
- It excludes zero and negative numbers.
8. Validate Negative Numbers
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
- typeof n === 'number' ensures the input is a number.
- n < 0 checks if the number is negative.
- It excludes zero and positive numbers.
9. Validate Even Numbers
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
- typeof n === 'number' ensures the input is a number.
- n % 2 === 0 checks if the remainder is zero when divided by 2.
- It works for both positive and negative numbers.
10. Validate Odd Numbers
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
- typeof n === 'number' ensures the input is a number.
- n % 2 !== 0 checks if the remainder is not zero when divided by 2.
- It works for both positive and negative numbers.
Similar Reads
JavaScript Number valueOf( ) Method JavaScript Number valueOf( ) method in JavaScript is used to return the primitive value of a number. This method is usually called internally by JavaScript and not explicitly in web code Syntax: number.valueOf() Parameters: This method does not accept any parameter. Return Value: The valueof() metho
1 min read
JavaScript Numbers JavaScript numbers are primitive data types, and unlike other programming languages, you don't need to declare different numeric types like int, float, etc. JavaScript numbers are always stored in double-precision 64-bit binary format IEEE 754. This format stores numbers in 64 bits:0-51 bits store t
6 min read
How numbers are stored in JavaScript ? In this article, we will try to understand how numbers are stored in JavaScript. Like any other programming language, all the data is stored inside the computer in the form of binary numbers 0 and 1. Since computers can only understand and process data in the form of 0's and 1's. In JavaScript, ther
6 min read
Convert a String to Number in JavaScript To convert a string to number in JavaScript, various methods such as the Number() function, parseInt(), or parseFloat() can be used. These functions allow to convert string representations of numbers into actual numerical values, enabling arithmetic operations and comparisons.Below are the approache
4 min read
JavaScript Numbers Coding Practice Problems Numbers are fundamental in JavaScript, used for calculations, data analysis, and algorithm implementation. This curated list of JavaScript number practice problems covers essential concepts to master JavaScript Numbers. Whether you're a beginner or looking to sharpen your numerical computation skill
1 min read