How to deal with a NaN in JS?
I have the following:
var cID = parseInt(hash.match(/\d+/g)[1]);
I then want to do something like so:
if (cID !== undefined) {
alert('hello');
}
Problem is cID if it does not find a match, is returning NaN in the console whe开发者_高级运维n logged... How do I create an if Statement, meaning if there is an INT from the match or not based on: parseInt(hash.match(/\d+/g)[1])
Thanks
if (isNaN(cID)){
//do stuff here
}
You might be looking for the global isNaN() function.
if (cID !== undefined || !isNaN(cID)) {
alert('hello');
}
Use isNaN javascript function to check if return value of parseInt is number or not:
var cID = parseInt(somevar);
if (isNaN(cID)) { alert ('error') };
docs for isNaN: https://developer.mozilla.org/en/Core_JavaScript_1.5_Guide/Functions#isNaN_Function
精彩评论