Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Numeric value 2

Status
Not open for further replies.

mitch77

Programmer
Nov 16, 2000
42
PT
Hi,
How can I view if a variable has a numeric value using javascript?


Thanks Mitch
 
actually it needs to be a lowercase "n" like:

if(typeOf(varname) == "number") jared@aauser.com
 
> How can I view if a variable has a numeric value using javascript?

> yeah yeah, whatever.... ;)

> if(typeOf(varname) == "number")

hehe, well actually you both missed the mark by a mile unless the original question was mis-stated.

"if a variable has a numeric value"
typeof() tests the type not the value B-)

function ( param){
if ( isNaN(param))
alert("Not a number value");
else
alert("Number value");
}

-pete
 
hey pete, i guess that you'll return false for a string which value is, say, "12"
sooooooooooooooo
the very proper way to check that is actually :
if (isNaN(parseInt(param, 10))) and then pete's code
where 10 is for the radix ;-)
 
x="10"
alert(isNaN(x))

returns true jared@aauser.com
 
yes, what i was saying, but alert(isNaN(ParseInt("10", 10)) should be false !!!
 
yes it dynamically converts between data types and adds wrapper objects when needed... jared@aauser.com
 
Actually parseInt() should return NaN if it cannot obtain a valid number from the string... so it is likely that the functions use the same algorithm. The main difference is that parseInt() allows different radix input where isNaN can only be used directly on base 10, i.e.:

isNaN("B2"); // true ( not a number )
isNaN(parseInt("B2", 16)); // false ( is a number)

-pete
 
that's not true at all:

x="105bxxbxv"
alert(isNaN(x))
x=parseInt(x)
alert(isNaN(x)) jared@aauser.com
 
they don't function the same at all. their usefulness is similar, but they function much differently jared@aauser.com
 
jaredn,

You are correct. While my example is correct my statement about using the same algorithm was not. Thanks for catching my error.

-pete
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top