Open In App

How to Check if a Variable is of Type Number in JavaScript ?

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

In JavaScript, you can check if a variable is of type number using the typeof operator. The typeof operator returns a string indicating the type of the operand.

Example: Here, typeof myVariable evaluates to the string 'number', and the === operator is used to check if it is equal to the string 'number'. If the condition is true, it means that the variable is of type number.

Javascript




let myVariable = 42;
 
if (typeof myVariable === 'number') {
    console.log('It is a number!');
} else {
    console.log('It is not a number.');
}


Output

It is a number!


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads