.
Actually there is no need to create an extra variable just and only to use it as a type modifier of the given value.
However you
DO need to validate that user inputs something that is number type and not any other kind of string.
var ageAsString = prompt("what is your age? ", "");
var age = Number(ageAsString);
var age = parseInt(prompt("what is your age?", ""));
if (isNaN(age)) {
alert("You have to insert a number mate")
} else {
alert(age);
if (age < 40) {
alert("You are so young...");
} else {
alert("Ok");
}
}
alert("thank you")
Now there is a catch, if you put let's say
23a
it will pass as a number and not a NaN.
For these cases you'll have to make more sophisticated validators but for now I think you have already got the idea of how to make your script more solid.
.