While working in any language, a general requirement is encountered to check if a string is a number. Most user inputs are taken in the form of strings and processed further after checking various attributes. In this article, let’s check if the given string is a number.

Table of Contents:-

Check if string is a number using RegEx

The simplest way to check if the string is a number or not is to use regular expressions, but one can use them to check if the string is a whole number.

Suppose the requirement is a little more complex. In that case, we should not use regular expressions, for example, to check for hexadecimal, octal, or exponential, or decimal values.

Example 1:-

Check if the string “-567845” is a number

Code:-

let dummyString = '-567845'
console.log( /^-?\d+$/.test(dummyString) )

Output:-

true

Example 2:-

Check if the string “567845.00” is a number

Code:-

let dummyString = '567845.00'
console.log( /^-?\d+$/.test(dummyString) )

Output:-

false

Check if string is a number using isNaN() and Number()

The Number.isNaN() method of javascript determines if the passed value is Not-A-Number.

The Number() function of javascript coverts a string or other value to the number type.

Function Code:-

function ifStringIsNumber(_string)
{
return !(Number.isNaN(Number(_string)))
}

Example 1:-

Check if the string “-567845” is a number

let dummyString = '-567845'
console.log(ifStringIsNumber(dummyString))

Output:-

true

Example 2:-

Check if the string “567845.00” is a number

let dummyString = '567845.00'
console.log(ifStringIsNumber(dummyString))

Output:-

true

Example 3:-

Check if the string “0xa” is a number

let dummyString = '0xa'
console.log(ifStringIsNumber(dummyString))

Output:-

true

Note that the input ‘-0xa’ will return false.

Check if string is a number using isNaN() and parseFloat()

The parseFloat() method of javascript parses an argument and returns a floating-point number.

Function Code:-

function ifStringIsNumber(_string)
{
return !(Number.isNaN(parseFloat(_string)))
}

Example 1:-

Check if the string “-567845” is a number

let dummyString = '-567845'
console.log(ifStringIsNumber(dummyString))

Output:-

true

Example 2:-

Check if the string “567845.00” is a number

let dummyString = '567845.00'
console.log(ifStringIsNumber(dummyString))

Output:-

true

Example 3:-

Check if the string 0xa is a number

let dummyString = '0xa'
console.log(ifStringIsNumber(dummyString))

Output:-

true

Check if string is a number using isNaN() and parseInt()

The parseInt() method of javascript parses an argument and returns an integer value.

Function Code:-

function ifStringIsNumber(_string)
{
return !(Number.isNaN(parseInt(_string)))
}

Example 1:-

Check if the string “-567845” is a number

let dummyString = '-567845'
console.log(ifStringIsNumber(dummyString))

Output:-

true

Example 2:-

Check if the string “567845.00” is a number

let dummyString = '567845.00'
console.log(ifStringIsNumber(dummyString))

Output:-

true

Example 3:-

Check if the string 0xa is a number

let dummyString = '0xa'
console.log(ifStringIsNumber(dummyString))

Output:-

true

Read More:

I hope this article helped you to check if the string is a number in javascript. Good Luck !!!