How do I check if a value is a number in JavaScript?
Alex K
alex k profile pic

In JavaScript, there are multiple approaches to check if a value is a number. Here's a step-by-step explanation of different methods you can use: 1. Using thetypeof operator: - Thetypeof operator returns a string that represents the type of a value. For numbers, it returns'number'. However, this method has some caveats, as it considersNaN (Not a Number) as a number and treatsInfinity and-Infinity as numbers as well.

1
2
3
4
5

   function isNumber(value) {
     return typeof value === 'number' && !isNaN(value);
   }
   

In this example, theisNumber function checks if thetypeof value is'number' and also verifies that the value is notNaN using theisNaN() function. 2. Using theisNaN() function: - TheisNaN() function determines whether a value isNaN (Not a Number). It returnstrue if the value isNaN, andfalse otherwise. However, note that it coerces the value to a number before performing the check, so non-numeric values may be converted toNaN before evaluation.

1
2
3
4
5

   function isNumber(value) {
     return typeof value === 'number' && !isNaN(value);
   }
   

This approach is similar to the previous one, where theisNumber function checks if the value is of type'number' and additionally ensures that it is notNaN. 3. Using theNumber.isFinite() function: - TheNumber.isFinite() function determines whether a value is a finite number. It returnstrue if the value is a finite number, andfalse otherwise. This method explicitly distinguishes between numbers andInfinity/-Infinity.

1
2
3
4
5

   function isNumber(value) {
     return typeof value === 'number' && Number.isFinite(value);
   }
   

In this approach, theisNumber function checks if the value is of type'number' and also confirms that it is a finite number using theNumber.isFinite() function. 4. Using regular expressions: - Regular expressions can be used to check if a value is a valid numeric string. This method allows you to verify if the value contains only numeric characters and, optionally, specific formatting rules.

1
2
3
4
5

   function isNumber(value) {
     return /^-?\d+(\.\d+)?$/.test(value);
   }
   

In this example, theisNumber function uses a regular expression pattern^-?\d+(\.\d+)?$ to match numeric strings. It checks if the value matches the pattern, allowing an optional negative sign and decimal part. Here's an example usage of theisNumber function:

1
2
3
4
5
6
7

console.log(isNumber(42)); // Output: true
console.log(isNumber('42')); // Output: true
console.log(isNumber('42.5')); // Output: true
console.log(isNumber('abc')); // Output: false
console.log(isNumber(NaN)); // Output: false
console.log(isNumber(Infinity)); // Output: false

In this example, theisNumber function is called with different values. It returnstrue for valid numbers and numeric strings andfalse for non-numeric values,NaN, and infinity. Choose the method that best suits your specific use case. Consider the requirements of your application and handle potential edge cases to ensure accurate number validation.