How To Check If String Is A Valid Number In Javascript

How To Check If String Is A Valid Number In Javascript

There are already built-in functions to check if string is a valid number in javascript. We will learn how to do this using four different methods. Each method is rather straightforward, so you can choose the method you often use.

Check if string is a valid number in javascript

Method 1: Using isNaN()

Syntax:

isNaN(value)

Parameter:

  • value: The value to be tested.

The isNaN() function determines whether a value is NaN or not (NaN means “Not-a-Number”). The isNaN() function returns a TRUE boolean value if a value is not a number. Hence, if the string is a number the method will return FALSE. For example:

let s = "123456";
console.log(isNaN(s));
let c = "abcdef";
console.log(isNaN(c));

Output:

false
true

The logic behind this method is very simple. If the string we are testing is a valid number, it will return false. Otherwise, it will return true, meaning the string is not a valid number.

Method 2: Using ‘+’ operator

Syntax:

+x

Parameter:

  • x: an operand, can be of any type such as string or number

If the string is a valid number, the unary plus operator (+ operator) returns the numeric value of the string represented. Otherwise, it will return null if the string isn’t a valid number:

let s = "123456";
console.log(+s);
let c = "abcdef";
console.log(+c);

The + operator attempts to convert the operand to a number if it is not already. If it cannot parse a particular value, it will evaluate to null.

Output: 

123456
null

The + operator method can help you check if the string is a valid number in just a line of code. Moreover, this method is well-known for its effective complexity, which means it doesn’t require the usage of any built-in function. Therefore, we highly recommend you use the unary + operator to check if string is a valid number in javascript.

Method 3: Using Number()

The Number() built-in function helps you convert any type into number type.

Syntax:

Number(value)

Parameter:

  • value: The numeric value of the object being created.

If the type of parameter you are passing is a valid number, it will return a value representing the numeric value of your parameter. Otherwise, it will return null meaning the parameter does not represent a valid number:

let s = "123456";
console.log(Number(s));
let c = "abcdef";
console.log(Number(c));

Output: 

123456
null

Method 4: Using parseInt()

Syntax:

parseInt(string)

Parameter:

  • string: A string starting with an integer. Leading whitespace in this argument is ignored.

Like the Number() function and + operator approach, the parseInt() function will return an integer parsed from the given string. Or returns null when the string cannot be converted into a valid number.

let s = "123456";
console.log(parseInt(s));
let c = "abcdef";
console.log(parseInt(c));

Output: 

123456
null

Summary

Using four different methods, we have learned how to check if string is a valid number in javascript. Although we have many ways to deal with this type of problem, we advise you to use the method of using unary + operator because it is the most efficient method.

Maybe you are interested:

Leave a Reply

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