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!