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 when 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
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 when 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
Share Improve this question asked Apr 26, 2011 at 20:49 AnApprenticeAnApprentice 111k202 gold badges636 silver badges1k bronze badges 1- 2 possible duplicate of How do you check that a number is NaN in JavaScript? – eldarerathis Commented Apr 26, 2011 at 20:52
4 Answers
Reset to default 12if (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/en/Core_JavaScript_1.5_Guide/Functions#isNaN_Function