From user input, I receive a string which must contain numbers. 0
and empty string must be treated differently.
I've made this brief case study
0 == "" // true
parseInt(0) == "" // true
parseInt("") // NaN
parseInt("") == "NaN" // false
parseInt("") == NaN // false
typeof parseInt("") // "number"
With a bit of extra research I've made up this solution (considering uInput
the user input)
function userInput(uInput){
var n = parseInt(uInput);
if (isNan(n))
// empty string
else
// 0 or number
}
It looks like it is able to distinguish 0 from empty string correctly in Google Chrome.
What I'd like to know is:
- Is this the best/most efficient solution?
- Is this solution compatible in any browser?
- Is this even a solution at all? Is there any way
n
can become something other than a number (consideringuInput
could be any kind of string)?
I wouldn't be asking this, but since empty string and 0 are treated the same (?!) I don't know to which other holes is my code potentialy exposed.