最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to deal with a NaN in JS? - Stack Overflow

programmeradmin7浏览0评论

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
Add a ment  | 

4 Answers 4

Reset to default 12
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/en/Core_JavaScript_1.5_Guide/Functions#isNaN_Function

发布评论

评论列表(0)

  1. 暂无评论