my dog learned polymorphism*
The moose likes HTML, CSS and JavaScript and the fly likes 2>11 results true with java script variables Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "2>11 results true with java script variables" Watch "2>11 results true with java script variables" New topic
Author

2>11 results true with java script variables

sharmi mekala
Greenhorn

Joined: Sep 04, 2008
Posts: 8
Hi All,

With the below code 2>11 results true which shouldnt be the case
one might ask me to initialise like x=2 and y=3 but when we take the values from text fields the values will be a string rather than an integer when we use document.getElementById()
Can someone please tell me why this is happening in Javascript. It was a crazy day for me to figure out why this was happening but i was clueless


<html>

<body>



<script type="text/javascript">



var x="2";

var y = "11";

alert(x>y)


</script>


</body>

</html>

David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

JavaScript most likely does an ASCII comparison for strings, which means "2" is greater than "11".

Maybe you should compare the values as numbers instead if you want them in numerical order.
sharmi mekala
Greenhorn

Joined: Sep 04, 2008
Posts: 8
Thanks for the reply David
But this is happening with only when x is in "1,2,3,4,5,6,7,8,9" and y is geater than 10
After than all other comparisions are giving the correct results. Why is this happening only when x<10 and y>10
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

I kinda already told you.My statement still stands, and you should compare numbers, not strings.
sharmi mekala
Greenhorn

Joined: Sep 04, 2008
Posts: 8
That makes sense David
But why should i write these many statements just because javascript doesnt understand the difference between a string and a number

var minValue=parseInt(document.getElementById(minField).value,10);
var maxValue=parseInt(document.getElementById(maxField).value,10);

if(isNaN(minValue))minValue=0;
if(isNaN(maxValue))maxValue=0;

The problem is parseInt treates empty string as NaN but I want that to be treated as 0 so i needed to write above 2 lines of code extra. (An end user doesnt care if he puts 0 or blank string when its not a mandatory field)
I know a blank string is 0 in case of integers but not javascript

sometimes we need to program in using roundabouts just because of the inconsistencies in programming language
I love programming but not this kind
Eric Pascarello
author
Rancher

Joined: Nov 08, 2001
Posts: 15376
    
    6
How would JavaScript know if you meant it to be a String or Number?

It does not assume it knows that if I have a String, I compare it as a String. If I have a Number, I compare it as a number.

I am guessing that
you would expect c to be 2 and not 11.

Eric
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60816
    
  65

sharmi mekala wrote:But why should i write these many statements just because javascript doesnt understand the difference between a string and a number

JavaScript knows the difference. It's you who do not.

var x = "2"; // is a string

var x = 2; // is a number

Don't blame the language because you aren't writing it correctly.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

I'd expect it to be "11" :)

@sharmi: So refactor and don't worry about it ever again.Of course, this (and your original code) has suspicious behavior if the user enters "12ah" or similar.
sharmi mekala
Greenhorn

Joined: Sep 04, 2008
Posts: 8
Hi Eric,

I understand that.

We need to write 4 statements to just convert a integer value from textfield to javascript variable of type int
I was just trying to figure out if we had a better way to do this

Anyways my question is answered - always we need to do an internal conversion to int in javascript when we take integer values from textfield

I was under an impression that while using arithmetic comparisions like > , < then javascript will try to convert variable values to integers and performs the operations( which was wrong )

Thank you all for clarifying my doubt

Finally learnt something today
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

You're still not quite there:If you don't *tell* JavaScript what to do it can only guess at your intentions.

And man, I sure wouldn't write four lines of code multiple times; that's a waste of time and cognitive overhead. If I have to do something more than once I refactor.
sharmi mekala
Greenhorn

Joined: Sep 04, 2008
Posts: 8
Thanks David

I think I got what you said
If one of the comparision operands is integer then it will convert the other into integer( is that what you meant) (2=="2" --> true)

Your code is working great. Thanks for helping me in refractoring the code
you are the best

Hi Bear

"Don't blame the language because you aren't writing it correctly. "

Dont take my words negatively. FYI I am not blaming the language but trying to understand the differences in languages.
System.out.println("x">"y");
when I say this in java it gives me an error saying that > operator is not allowed for type string,string
I was expecting the same in javascript which wasnt the case.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

JavaScript and Java are very different languages, despite some external similarities. It's far, far more dynamic--you'd be best served by learning JavaScript without the baggage of other languages: treat it as something new and learn how to do things the JavaScript way.
sharmi mekala
Greenhorn

Joined: Sep 04, 2008
Posts: 8
Yes David. All these days I was under the impression like Java is similar to Javascripts but not any more
until we get a bug we never dig into concepts which shouldnt be the case
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Just don't get any bugs then and you'll be fine ;)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: 2>11 results true with java script variables
 
Similar Threads
Ajax, Struts, and a few pesky event based issues
MultiDimenaional Array sort
readOnly Attribute
Having trouble with a javascript assignment
Object does not support property error!