As far as I know, Prompt returns only String type of value. Then, how does the below code work properly?
var a = prompt('Number or String? Verify now!');
if(!isNaN(a)){
alert('This is number.');
}
else alert('This is string.')
As far as I know, Prompt returns only String type of value. Then, how does the below code work properly?
var a = prompt('Number or String? Verify now!');
if(!isNaN(a)){
alert('This is number.');
}
else alert('This is string.')
From https://www.w3schools.com/jsref/jsref_isNaN.asp, The global isNaN() function, converts the tested value to a Number, then tests it.
The isNaN() function determines whether a value is an illegal number (Not-a-Number).
This function returns true if the value equates to NaN. Otherwise it returns false.
This function is different from the Number specific Number.isNaN() method.
The global isNaN() function, converts the tested value to a Number, then tests it.
Number.isNaN() does not convert the values to a Number, and will not return true for any value that is not of the type Number.
isFinite(value) converts its argument to a number and returns true if it’s a regular number, not NaN/Infinity/-Infinity:
alert( isFinite("15") ); // true
alert( isFinite("str") ); // false, because a special value: NaN
Sometimes isFinite is used to validate whether a string value is a regular number so check this code
var a = prompt('Number or String? Verify now!');
if(isFinite(a)){
alert('This is number.');
}
else alert('This is not number.')