# How to check if a variable is a number in JavaScript?

Are you stuck building logic to check whether a variable is a number or not? JavaScript has various methods to help you out in this process. But first, let’s review variables. Variables are containers for storing data or you can say that it is named storage for data. For example –

`var state = "Haryana";`

Code language: JavaScript (javascript)

Data called `Haryana`

got stored in the box called `state`

. When the variable is redeclared or updated, the old value is removed and the new value is stored in its place.

There are three ways in which you can declare variables in JavaScript-

**var**– It is global and it can be reset, or reassigned. It is rarely used anymore**const**– If you want to declare a variable that will not be modified then it is used. It can be reset or reassigned, it is immutable**let**– It can be modified but cannot be reset. It can be reassigned and have a block context

Variables declared with `let`

and `const`

are **hoisted** but unlike `var`

are not initialized with a default value. That’s why variables declared with `let`

and `const`

cannot be accessed before their declaration.

Note-**Hoisting** in JavaScript is a process whereby variables and function declarations are moved to the top of their scope before code execution.

Since JavaScript is a dynamically typed language, the type of the variable is determined by the interpreter at runtime meaning that we are not always aware of the type of the variable in our code while using it. This can bring unexpected results to our code. Also, a variable can be used without declaring it. The variable which is used without declaration automatically becomes a global variable.

While building something or solving a question in JavaScript, you might want to validate whether a variable is a number or not. Traditionally, “10” should not be accepted as a number. In JavaScript, `NaN`

, `infinity`

, and `-infinity`

are numbers too! We will list out some ways which will prove to be helpful to you.

## Using Number.isNaN()

In `Number.isNaN()`

, the type of input value is checked with the `Number`

type. The `Number`

object has a method called `isNaN()`

. The `NaN`

is a global property that represents **Not-A-Number**. It represents a computational error resulting from an incorrect or undefined mathematical operation. We will use `!`

(NOT operator) in our checks as we want to check whether the variable is a number.

For example –

```
let intVariable = 2;
let floatVariable = 10.5;
let stringVariable = '4';
let nanVariable = NaN;
let infinityVariable = Infinity;
let nullVariable = null;
let undefinedVariable = undefined;
> !Number.isNaN(intVariable);
true
> !Number.isNaN(floatVariable);
true
> !Number.isNaN(stringVariable);
true # Wrong
> !Number.isNaN(nanVariable);
false
> !Number.isNaN(infinityVariable);
true # Wrong
> !Number.isNaN(nullVariable);
true # Wrong
> !Number.isNaN(undefinedVariable);
true # Wrong
```

Code language: JavaScript (javascript)

As you can observe, `Number.isNaN()`

returns true for some values that are not numbers at all, that’s why this method is best suited when you only want to check for a `NaN`

value. For general number checking, this method may not give the best results.

It is important to note that `Number.isNaN()`

and `isNaN()`

are not the same, the global `isNaN()`

returns true for some unexpected values- `({}, "hello", undefined)`

, while `Number.isNaN()`

will only return true for the actual `NaN`

. If the value is not a number, `isNaN`

converts the value to a number, to avoid coercion from happening, `Number.IsNaN()`

is preferred.

## Using Number.isfinite()

`Number.isfinite()`

checks for numbers as well as infinite values in a variable. So, when you have numbers that are `NaN`

, `infinity`

, or `-infinity`

, it returns false.

```
let intVariable = 2;
let floatVariable = 10.5;
let stringVariable = '4';
let nanVariable = NaN;
let infinityVariable = Infinity;
let nullVariable = null;
let undefinedVariable = undefined;
> Number.isFinite(intVariable);
true
> Number.isFinite(floatVariable);
true
> Number.isFinite(stringVariable);
false
> Number.isFinite(nanVariable);
false
> Number.isFinite(infinityVariable);
false
> Number.isFinite(nullVariable);
false
> Number.isFinite(undefined);
false
```

Code language: JavaScript (javascript)

The result is correct in all cases.

It is important to know that `Number.isFinite()`

is different and more preferred than `isFinite()`

as the global `isFinite`

coerces non-numbers to numbers, returning true for anything that coerces to a finite number.

## Using typeof()

JavaScript has another global function called `typeof()`

which takes a value as an argument, returning the string representation of its type.

There are total 9 `types`

in JavaScript –

- number
- string
- boolean
- undefined
- bigint
- object
- null
- function
- symbol

```
let intVariable = 2;
let floatVariable = 10.5;
let stringVariable = '4';
let nanVariable = NaN;
let infinityVariable = Infinity;
let nullVariable = null;
let undefinedVariable = undefined;
> typeof(intVariable) == 'number';
true
> typeof(floatVariable) == 'number';
true
> typeof(stringVariable) == 'number';
false
> typeof(nanVariable) == 'number';
true # Wrong
> typeof(infinityVariable) == 'number';
true # Wrong
> typeof(nullVariable) == 'number';
false
> typeof(undefined) == 'number';
false
```

Code language: JavaScript (javascript)

The `typeof()`

method performs much better as it correctly determines the type of the variable. It did not identify the string variable, `undefined`

and `null`

are not numbers. For `NaN`

and `infinity`

, `typeof()`

method returns true. Both `NaN`

and `infinity`

are special number values.

## Conclusion

We saw various methods that can help us determine whether the variable is a number or not. `Number.isNaN()`

is best suited if you’re testing your variable for `NaN`

and you are not doing general checking.`typeof()`

is best suited if you can work with `infinity`

, –`infinity`

, and `NaN`

. It works much better than `Number.isNaN()`

.`Number.isfinite()`

proved to be the most appropriate one of these methods.

Hope you have understood the concept 🙂

Thanks for reading!

Sharing is caring

Did you like what Vineeta Tiwari wrote? Thank them for their work by sharing it on social media.

## No comments so far

Curious about this topic? Continue your journey with these coding courses: