[thelist] javascript comparison

Jeff Howden jeff at jeffhowden.com
Wed Oct 29 12:38:57 CST 2003


rob,

><><><><><><><><><><><><><><><><><><><><><><><><><><><><><
> From: Rob Smith
>
> My javascript test is testing supposedly numerical
> values as strings, when they should be compared by
> actual value.
><><><><><><><><><><><><><><><><><><><><><><><><><><><><><

yup, javascript is typeless, so it thinks numbers are strings, unless
explicitly told otherwise.

><><><><><><><><><><><><><><><><><><><><><><><><><><><><><
> if (bid.previousbid.value > bid.nextbid.value )
>
> [snip]
>
> Is there a way I can cast values in javascript?
><><><><><><><><><><><><><><><><><><><><><><><><><><><><><

not really, but you can force the parser to treat a value as a number using
the parseInt() function.

if(parseInt(bid.previousbid.value, 10) > parseInt(bid.nextbid.value, 10))

the second argument, radix, is technically optional, but i always include it
just to be on the safe side.  if you don't include it then you run the risk
of using it with a number that starts with a 0 (zero) and the system
thinking it's a base 8 (i think) number.

good luck,

.jeff

——————————————————————————————————————————————————————
Jeff Howden - Web Application Specialist
Résumé - http://jeffhowden.com/about/resume/
Code Library - http://evolt.jeffhowden.com/jeff/code/



More information about the thelist mailing list