Typescript check is number

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()

Читайте также:  Java import java nio file files

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

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

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.

Источник

How To Check If String Is A Valid Number In TypeScript

Check if String is a Valid Number in TypeScript

When working with strings, there are many times that you will have to check if string is a valid number in TypeScript. If you are still struggling and don’t know how to do so, please check out our tutorial below for some of the most common methods.

Check If String Is A Valid Number In TypeScript

Using Number.isNaN() method

First, let’s see what is a valid string to be converted into a valid number.

The Number() method will convert any valid value into a number.

Return value:

  • If the value is a valid string, the return value is a number corresponding to the string.
  • If the value is a boolean, the return value is 0 or 1.
  • If the value is a date, the return value is milliseconds since January 1st, 1970.
var myString = '1234567'; var myBool = true; var myDate = new Date(); var number1 = Number(myString); var number2 = Number(myBool); var number3 = Number(myDate); console.log(number1); console.log(number2); console.log(number3);

However, let’s take a look if the string value is not valid:

var myString = '1j82390'; var num = Number(myString); console.log(num); console.log(typeof num);

The Number() method will return ‘nan’, which means ‘not a number’. Therefore, to check if string is a valid number, we will check if it is NaN or not. And JavaScript has supported us with the Number.isNaN() method.

Return value:

True if it is a valid number and false if it is not.

Now let’s apply it to solve our problem:

var myString1 = "1j82390"; var myString2 = "34839"; var number1 = Number(myString1); var number2 = Number(myString2); function checkNum(num: any) < if (Number.isNaN(num)) < console.log("This string is not a valid number!"); >else < console.log("This string is a valid number!"); >> checkNum(number1); checkNum(number2);
This string is not a valid number! This string is a valid number!

Using isNaN() method

When using the Number.isNaN() method, firstly, you have to convert the string into a number. However, there is one more built-in method that doesn’t require you to do that first step: the isNaN() method.

Return value:

True if it is a valid number and false if it is not.

Basically, isNaN() will convert the value into a number before checking, so you don’t have to use the Number() method.

var myString1 = "1j82390"; var myString2 = "34839"; function checkNum(num: any) < if (isNaN(num)) < console.log("This string is not a valid number!"); >else < console.log("This string is a valid number!"); >> checkNum(myString1); checkNum(myString2);
This string is not a valid number! This string is a valid number!

Summary

In this tutorial, we showed you two common ways to check if string is a valid number in TypeScript. The main idea is to use the Number.isNaN() and isNaN() method. If you have any questions, please comment below. Thanks for reading!

Maybe you are interested:

Hello. My name is Khanh Hai Ngo. I graduated in Information Technology at VinUni. My advanced programming languages include C, C++, Python, Java, JavaScript, TypeScript, and R, which I would like to share with you. You will benefit from my content.

Name of the university: VinUni
Major: EE
Programming Languages: C, C++, Python, Java, JavaScript, TypeScript, R

Источник

isNumber

Use typeof to check if a value is classified as a number primitive. To safeguard against NaN , check if val === val (as NaN has a typeof equal to number and is the only value not equal to itself).

const isNumber = (val: any) => typeof val === "number" && val === val; 
isNumber(1); // true isNumber("1"); // false isNumber(NaN); // false 

Источник

Оцените статью