Open In App

How to Check if a Value is a Number in JavaScript ?

Last Updated : 19 Jan, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

In JavaScript, if you try to add a number with a number string i.e. 3+”2″ = 32, will result in a concatenated string. To avoid such scenarios you have to perform the type checks to check if the value you are using is a number or not.

We can use the below methods to check whether the value is a number or not:

Using the isNaN() method

The isNaN() method in JavaScript is used to check whether the passed value is Not a Number. It returns true if the passed number is Not a Number. Otherwise, it will return false.

NOTE: The isNaN() method can successfully parse a numeric string(“123”) as a number and returns false, which means it considers a numeric string as a number.

Syntax:

isNaN(testingValue);

Example: The below code implements the isNaN() method to check whether the passed value is a number.

Javascript




function isNumber(value){
    try{
        if(isNaN(value)){
            throw new Error("Passed value is not a number");
        }
        console.log("Passed value is a number");
    }
    catch(error){
        console.log("An error occurred: ", error.message);
    }
}
 
isNumber(55);
isNumber("58");
isNumber("GeeksforGeeks");


Output

Passed value is a number
Passed value is a number
An error occurred:  Passed value is not a number

Using the typeof operator

The typeof operator can be used to perform the type check of the passed value and compare it with the number type to check whether it is a number or not.

Syntax:

typeof testingValue;

Example: The below code example uses the typeof operator to check for the type of the passed value.

Javascript




function isNumber(value){
    try{
        if(!(typeof value === 'number')){
            throw new Error("Passed value is not a number");
        }
        console.log("Passed value is a number");
    }
    catch(error){
        console.log("An error occurred: ", error.message);
    }
}
 
isNumber(123);
isNumber("123");
isNumber("GFG");


Output

Passed value is a number
An error occurred:  Passed value is not a number
An error occurred:  Passed value is not a number

Using the Number.isInteger() method

The Number.isInteger() method can also be used to check if a value is number or not. It is mainly used for checking the integer values.

Syntax:

Number.isInteger(testingValue);

Example: The below code explains the use of the Number.isInteger() method to check if a value is number or not.

Javascript




function isNumber(value){
    try{
        if(!(Number.isInteger(value))){
            throw new Error("Passed value is not a number");
        }
        console.log("Passed value is a number");
    }
    catch(error){
        console.log("An error occurred: ", error.message);
    }
}
 
isNumber(69);
isNumber("231");
isNumber("Geek");


Output

Passed value is a number
An error occurred:  Passed value is not a number
An error occurred:  Passed value is not a number

Using the Number.isFinite() method

The Number.isFinite() method is used to check if the passed value is finite or not. If it unables to parse the value it returns false and if it parses the value then it check if it is finit or infinite.

Syntax:

Number.isFinite(testingValue);

Example: The below code shows the practical implementation of the Number.isFinite() method to check if it is number.

Javascript




function isNumber(value){
    try{
        if(!(Number.isFinite(value))){
            throw new Error("Passed value is not a number");
        }
        console.log("Passed value is a number");
    }
    catch(error){
        console.log("An error occurred: ", error.message);
    }
}
 
isNumber(108);
isNumber("11");
isNumber("Hello");


Output

Passed value is a number
An error occurred:  Passed value is not a number
An error occurred:  Passed value is not a number


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads