Jump to content

Comparision


raghunathmitte
 Share

Recommended Posts

try{String quantity=parseInt("abc123");if(quantity == NaN)throw "quantity cannot be null";}catch(exception){ alert(exception);}i have the above code in a function when i try to convert the string into int parseInt() method returns NaN value but if condition is not executing, is there any other way to compare with NaN?? please help me out

Link to comment
Share on other sites

You don't even need to use try ... catch (which is memory-intensive):

var quantity = "abc123";if (!isNaN(quantity)) quantity = parseInt(quantity);else alert("Quantity is Not a Number!");

Link to comment
Share on other sites

parseInt cannot handle letter in front of the number, only behind the number.
Sure it can - as long as you are using a base that is greater than 10:
var str = "rundmc";var num = parseInt(str, 36);alert(num);

Link to comment
Share on other sites

Sure it can - as long as you are using a base that is greater than 10:
var str = "rundmc"; var num = parseInt(str, 36); alert(num);

somehow i doubt that's what the OP had in mind...and thinking a little more about it, you are wrong.in your example, "rundmc" is a number, so there once again are no letters in front of the number...
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...