JavaScript/TypeScript – Check if Variable is a Number

Learn to check if a given variable’s value is a number or not, using different solutions such as Number.isFinite() and typeof() functions. The given solutions work with JavaScript as well as TypeScript.

1. Using JavaScript Number object

1.1. Checking a Number with Number.isFinite()

The Number class is part of ES6, so it is also available in TypeScript. The Number.isFinite() determines whether the passed value is a finite number i.e. value is neither positive Infinity, negative Infinity, nor NaN.

Number.isFinite() Also checks for other edge cases such as null, undefined and empty strings.

Number.isFinite(0);		//true
Number.isFinite(100);		//true
Number.isFinite(-100);		//true
Number.isFinite(100.12);	//true
Number.isFinite(-100.12);	//true

Number.isFinite('');				//false
Number.isFinite('100');				//false
Number.isFinite(null);				//false
Number.isFinite(undefined);		//false
Number.isFinite(0 / 0);			//false
Number.isFinite(Infinity);			//false
Number.isFinite(-Infinity);			//false
Number.isFinite(NaN);				//false
Number.isFinite(true);				//false
Number.isFinite(false);			//false

1.2. Checking an Integer with Number.isInteger()

Note that if we strictly check for integer types, we can use the Number.isInteger() function that determines whether the passed value is an integer.

Also, notice that if the value has a fraction part as .00 , it will also be considered an integer value. For other edge cases, such as null, undefined etc., the result will be similar to Number.isFinite() function.

Number.isInteger(0);			//true
Number.isInteger(100);		//true
Number.isInteger(-100);		//true
Number.isInteger(100.00);		//true - Needs special attention

Number.isInteger(100.12);		//false
Number.isInteger('100');		//false

//Other edge cases are similar to Number.isFinite()

1.3. Parsing a String to Number with Number.parseFloat()

In some cases, we get the value as string from the API response and want to validate if the value is a valid number. In this case, we can use Number.parseFloat() parses an argument and returns a floating point number.

If an argument cannot be parsed, it returns NaN.

Number.parseFloat(0);				//0
Number.parseFloat(10);				//10
Number.parseFloat(-10);				//-10
Number.parseFloat(10.00);				//10
Number.parseFloat(10.12);				//10.12

Number.parseFloat('0');				//0
Number.parseFloat('10');				//10
Number.parseFloat('-10');				//-10
Number.parseFloat('10.00');			//10
Number.parseFloat('10.12');			//10.12

//For other cases it returns NaN
Number.isFinite('');					//NaN
Number.isFinite(null);					//NaN
Number.isFinite(undefined);			//NaN

2. Using typeof() Function

The typeof() function is used to find the data type of a JavaScript variable. If typeof function returns the type as 'number' for number and integer values.

We need to put special checks in the code for NaN, positive Infinity and negative Infinity because these all are of type number.

typeof(0)				//number
typeof(10)				//number
typeof(-10)				//number
typeof(10.12) 				//number
typeof(-10.12) 			//number

//Special checks needed for these constants

typeof(NaN)			//number
typeof(Infinity)		//number
typeof(-Infinity)		//number

3. Conclusion

In this ES6 tutorial, we learned to check if a given variable is a number or not in JavaScript and TypeScript. We learned to check for the edge cases including empty strings, null, undefined and NaN values.

Using the Number.isFinite() is the recommended approach because if returns the most appropriate values in all normal and edge cases.

Happy Learning !!

Comments

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments

About Us

HowToDoInJava provides tutorials and how-to guides on Java and related technologies.

It also shares the best practices, algorithms & solutions and frequently asked interview questions.

Our Blogs

REST API Tutorial

Dark Mode

Dark Mode