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. 

Syntax:

Number(value)

Parameters

  • value: the valid value that will be converted 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. 

Example: 

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

Output: 

1234567
1
1664649074999

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

Output:

nan
number

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.

Syntax:

Number.isNaN(value)

Parameters:

  • Value: the value that you want to check

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

Output: 

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.

Syntax:

isNaN(value)

Parameters:

  • Value: the value that you want to check

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.

Completed code: 

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

Output: 

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:

Leave a Reply

Your email address will not be published. Required fields are marked *