# [Solved-5 Solutions] Is there a (built-in) way in javascript to check if a string is a valid number ? - javascript tutorial

## Problem:

Is there a built-in way in javascript to check if a string is a valid number ?

## Solution 1:

To check if a variable (including a string) is a number, check if it is not a number:
This works regardless of whether the variable contains is a string or number.

For Example:

To implement the `IsNumeric`:

To convert a numeric value of the sting: It only works if the string contains numeric characters, else it returns NaN.

Example:

To convert a string loosely to a number which is useful for converting '11px' to 11, for example.

Example:

Floats: Keep in mind that, unlike +num, parseInt (as the name suggests) will convert a float into an integer by cutting off everything following the decimal point (if we want to use parseInt() because of this behaviour, you're possibly better off with Math.floor() instead):

Empty strings may be a little counter-intuitive. +num converts empty strings to zero, and isNaN()assumes the same:

But parseInt() does not agree:

Python String Method.

Using RegExp:

## Solution 3:

To check if a string is a whole number (no decimal places).

Allows positive whole numbers.

Python String Format.

## Scientific Notation:

Scientific notation `!isNaN('1e+30')` is true.

## Large floating numbers:

Using Node.js:

On the other side:

String(Number(s)) === s, limit the strings to 16 digits at most (after omitting leading zeros).

## Infinity

To check the given string is a number satisfying all of the following:

• Non scientific notation
• Predictable conversion to Number and back to String
• Finite