How to Check if a Variable is of Type Number in JavaScript ?
Last Updated :
29 Jan, 2024
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.' );
}
|
Share your thoughts in the comments
Please Login to comment...