开发者

How to deal with a NaN in JS?

开发者 https://www.devze.com 2023-02-28 17:42 出处:网络
I have the following: var cID = parseInt(hash.match(/\\d+/g)[1]); I then want to do something like so:

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

0

精彩评论

暂无评论...
验证码 换一张
取 消