473,383 Members | 1,829 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,383 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 20998
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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.