how to write if variable is a number then in js code example
Example 1: javascript check if variable is number
function isNumber(n) {
return !isNaN(parseFloat(n)) && !isNaN(n - 0);
}
Example 2: check if a variable is a number in javascript
// as test cases we have two variables of which 1 is a number and the second one is a string
let x = 7;
let y = "hello"
let result1 = !Number.isNaN(x) // true
let result2 = !Number.isNaN(y) // flase