**JavaScript: Check if Variable is a Number**

### Introduction

JavaScript is a dynamically typed language, meaning that the interpreter determines the type of the variable at runtime. In practice, this allows us to use the same variable to store different types of data in the same code. It also means that without documentation and consistency, we do not always know the type of a variable in our code when we use it.
Operating on a string or array when we expect a number can bring strange results in our code. In this article, we'll look at various functions that can help determine if a variable we are using is a number.
Strings that contain numbers like "10" should not be accepted. In JavaScript, special values like `NaN`

, `Infinity`

and `-Infinity`

are numbers too - though, we'll be ignoring those values.
With these requirements, the best function to use is the `isFinite()`

function from the built-in `Number`

object.
However, developers have commonly used other functions for this purpose, particularly `Number.isNaN()`

and the `typeof()`

function.
Let's create a few variables for testing:

```
let intVar = 2;
let floatVar = 10.5;
let stringVar = '4';
let nanVar = NaN;
let infinityVar = Infinity;
let nullVar = null;
let undefinedVar = undefined;
```

### Using the *Number.isFinite()* function

The `Number.isFinite()`

function checks if the variable is a number, but also checks if it's a finite value. Therefore, it returns `false`

on numbers that are `NaN`

, `Infinity`

or `-Infinity`

.
Let's test it out on the variables we've defined above:

```
> Number.isFinite(intVar);
true
> Number.isFinite(floatVar);
true
> Number.isFinite(stringVar);
false
> Number.isFinite(nanVar);
false
> Number.isFinite(infinityVar);
false
> Number.isFinite(nullVar);
false
> Number.isFinite(undefined);
false
```

It's exactly what we wanted. The special non-finite numbers are ignored, as well as any variable that isn't a number type.
If you'd like to check if a variable is a number, your best bet is to use the `Number.isFinite()`

function.

### Using the *Number.isNaN()* Function

The standard `Number`

object has an `isNaN()`

method. It takes one argument, and determines if its value is `NaN`

. Since we want to check if a variable is a number, we will use the not operator, `!`

, in our checks.
Now let's check if the not operator and `Number.isNaN()`

function can filter only numbers:

```
> !Number.isNaN(intVar);
true
> !Number.isNaN(floatVar);
true
> !Number.isNaN(stringVar);
true # Wrong
> !Number.isNaN(nanVar);
false
> !Number.isNaN(infinityVar);
true # Wrong
> !Number.isNaN(nullVar);
true # Wrong
> !Number.isNaN(undefinedVar);
true # Wrong
```

This method is a fairly permissive as it accepts values that are not numbers at all. This method is best suited for when you know you have a number and would to check if it is a `NaN`

value, not for general number checking.

### Using the *typeof()* function

The `typeof()`

function is a global function that accepts a variable or value as an argument and returns a string representation of its type. JavaScript has 9 types in total:

`undefined`

`boolean`

`number`

`string`

`bigint`

`symbol`

`object`

`null`

(`typeof()`

shows as object)`function`

(a special type of object)

To verify if a variable is a number, we simply we need to check if the value returned by `typeof()`

is `"number"`

. Let's try it out on test variables:

```
> typeof(intVar) == 'number';
true
> typeof(floatVar) == 'number';
true
> typeof(stringVar) == 'number';
false
> typeof(nanVar) == 'number';
true # Wrong
> typeof(infinityVar) == 'number';
true # Wrong
> typeof(nullVar) == 'number';
false
> typeof(undefined) == 'number';
false
```

The `typeof()`

function performs much better than `Number.isNaN()`

. It correctly determines that a string variable, `null`

and `undefined`

are not numbers. However, it returns true for `NaN`

and `Infinity`

.
While that is the technically correct result, `NaN`

and `Infinity`

are special number values, for most use cases we would prefer to ignore them.

### Conclusion

In this article, we learned how to check if a variable in JavaScript is a number. The `Number.isNaN()`

function is only suitable if we know our variable is a number and need to verify if it specifically `NaN`

or otherwise.
The `typeof()`

function is suitable if your code can work with `NaN`

, `Infinity`

or `-Infinity`

as well as other numbers.
The `Number.isFinite()`

method captures all the finite numbers and was most appropriate for our requirements.

**Reference: stackabuse.com**