473,399 Members | 4,192 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

compare string with numbers in javascript

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!!
Jul 23 '05 #1
3 20999
jr********@hotmail.com (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 - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 23 '05 #2
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 <gw*****@agricoreunited.com>
comp.lang.javascript FAQ - http://jibbering.com/faq

Jul 23 '05 #3
Grant Wagner <gw*****@agricoreunited.com> 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 - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 23 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: J.W. | last post by:
How do I compare strings in javascript? The "==" double equals or "!=" doesn't seem to work in this case. I'm sure string comparison has been explained before but searching Google didn't find...
4
by: Matt | last post by:
I posted this problem before but no reply. Now I repost again. I want to compare the address number in javascript, and the address number is alphanumeric. I have a text box and the user needs to...
9
by: **Developer** | last post by:
I do the following: Select Case mDataType(mColumnToSort) Case TypeCode.Int32 Result = CInt(xText).CompareTo(CInt(yText)) Case TypeCode.String
12
by: Assimalyst | last post by:
Hi, I have a working script that converts a dd/mm/yyyy text box date entry to yyyy/mm/dd and compares it to the current date, giving an error through an asp.net custom validator, it is as...
17
by: Mark A | last post by:
DB2 8.2 for Linux, FP 10 (also performs the same on DB2 8.2 for Windoes, FP 11). Using the SAMPLE database, tables EMP and EMLOYEE. In the followng stored procedure, 2 NULL columns (COMM) are...
11
by: balakrishnan.dinesh | last post by:
hi frnds, Im having two 20digit numbers, But while comparing those it is giiving wrong ouput in javascript. for example here is my code, my secanrio is , ~ If first 20 digit number is...
3
by: whisher | last post by:
Hi. I've got this simple snippets. /*function isTheSame(string1,string2) { return (string1.toString() == string2.toString()) }*/ function isTheSame(string1,string2) { string1 = new...
11
by: Marshall Dudley | last post by:
I am comparing two numbers, and the comparison gives absolutely bizzare results. I am comparing 5 with a number that is entered in a form. Then if the number entered is 5, or between 50 and 95, it...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.