How to check if a prompt is a number? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 5

How to check if a prompt is a number?

Help

16th Apr 2021, 11:17 AM
Hello
Hello - avatar
6 Answers
+ 4
Typeof return value of prompt is always string.
16th Apr 2021, 11:34 AM
TOLUENE
TOLUENE - avatar
+ 2
// chech.min.js alert(!isNaN(prompt('Type a number')));
16th Apr 2021, 11:37 AM
Basel.Al_hajeri?.MBH()
Basel.Al_hajeri?.MBH() - avatar
+ 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 }
16th Apr 2021, 11:33 AM
Basel.Al_hajeri?.MBH()
Basel.Al_hajeri?.MBH() - avatar
+ 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.
17th Apr 2021, 5:14 PM
Nitesh
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!');
29th Sep 2023, 2:31 AM
Jack
Jack - avatar
0
Type of return value of string is always string const number = '1234'; console.log(number);
10th Nov 2023, 1:15 PM
ALI ASGHAR TANVEER GUJJAR
ALI ASGHAR TANVEER GUJJAR - avatar