+ 5
How to check if a prompt is a number?
Help
6 Antworten
+ 4
Typeof return value of prompt is always string.
+ 2
// chech.min.js
alert(!isNaN(prompt('Type a number')));
+ 1
let pro = +prompt('Type a number!, 0); // unary + convert it to a number
if(isNaN(pro)) { // NaN means Not a Number check if it is
alert('It is not a number');
} else {
// Here it is a number
}
+ 1
To check the typeof prompt first store it's value in a variable or else you could type - console.log(typeof prompt("Value")); But the type of prompt is always a string.
0
There is the one "grandfather`s method":
input = prompt('Enter a number');
if(Number(input)%1===0) console.log('Number!');
else console.log('String!');
0
Type of return value of string is always string
const number = '1234';
console.log(number);