Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > compare string with numbers in javascript

Reply
Thread Tools

compare string with numbers in javascript

 
 
Matt
Guest
Posts: n/a
 
      09-12-2004
if (123 > 33) will return true

and

if ("123" > 33) will return true

So my question is, if the above behaviors are the same?? If string is
a number, and
compare with another number, it will be the same behavior as compare 2
numbers?

In this case, it is comparing 2 strings that are numbers, so they are
string comparisons here.
correct?

if ("123" > "33") will return true

In this case, "33a" is not a number, that's why when it compare with
another number, it
always return false. correct?

if ("33a" > 33) will return false

please advise. thanks!!
 
Reply With Quote
 
 
 
 
Lasse Reichstein Nielsen
Guest
Posts: n/a
 
      09-12-2004
http://www.velocityreviews.com/forums/(E-Mail Removed) (Matt) writes:

> if (123 > 33) will return true
>
> and
>
> if ("123" > 33) will return true


Correct.

> So my question is, if the above behaviors are the same?? If string
> is a number, and compare with another number, it will be the same
> behavior as compare 2 numbers?


Yes. The "greater than" comparison operator, when given a number and
another argument, will convert the other argument to a number. The
conversion is the same as what the Number function does.

> In this case, it is comparing 2 strings that are numbers, so they are
> string comparisons here.
> correct?


Correct. If *both* arguments are strings, the comparsion is lexical
comparison.

> if ("123" > "33") will return true


The result of ("123" > "33") is false, since the first character of
the first string is smaller than the first character of the second
string.

> In this case, "33a" is not a number, that's why when it compare with
> another number, it always return false. correct?


Correct, because (Number("33a")) yields NaN, and (NaN > 33) is false.

/L
--
Lasse Reichstein Nielsen - (E-Mail Removed)
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
 
Reply With Quote
 
 
 
 
Grant Wagner
Guest
Posts: n/a
 
      09-13-2004
Lasse Reichstein Nielsen wrote:

> Correct, because (Number("33a")) yields NaN, and (NaN > 33) is false.


Actually, (Number.NaN < 33) is false as well. As is (Number.NaN == Number.NaN).

(Number.NaN != Number.NaN) is true, which goes against common sense, but it's
the documented behaviour:

<url:
http://devedge.netscape.com/library/...r.html#1193302
/>
<url: http://msdn.microsoft.com/library/en...onannumber.asp
/>

--
Grant Wagner <(E-Mail Removed)>
comp.lang.javascript FAQ - http://jibbering.com/faq

 
Reply With Quote
 
Lasse Reichstein Nielsen
Guest
Posts: n/a
 
      09-13-2004
Grant Wagner <(E-Mail Removed)> writes:

> Actually, (Number.NaN < 33) is false as well. As is (Number.NaN ==
> Number.NaN).
>
> (Number.NaN != Number.NaN) is true, which goes against common sense,


I'd say that (NaN == NaN) being false is what goes against common sense.
From that, I find it entirely logical that
(NaN != NaN) is the same as !(NaN == NaN)

/L
--
Lasse Reichstein Nielsen - (E-Mail Removed)
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Compare, two identical numbers are not the same?! Justin C Perl Misc 8 07-04-2009 10:50 PM
searching an array, string compare functions, string sorting ckirchho@directmedia.de Javascript 2 10-10-2007 11:23 PM
compare string and "" string literal lovecreatesbea...@gmail.com C++ 10 01-24-2007 09:55 PM
How to compare two 20 digit numbers in javascript balakrishnan.dinesh@gmail.com Javascript 11 08-03-2006 11:22 AM
strcmp vs. string::compare(const string &) lchian@yahoo.com C++ 10 03-03-2006 08:53 PM



Advertisments