• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

2>11 results true with java script variables

 
sharmi mekala
Greenhorn
Posts: 8
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12617
IntelliJ IDE Ruby
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12617
IntelliJ IDE Ruby
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I kinda already told you.My statement still stands, and you should compare numbers, not strings.
 
sharmi mekala
Greenhorn
Posts: 8
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15385
6
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64204
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12617
IntelliJ IDE Ruby
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12617
IntelliJ IDE Ruby
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12617
IntelliJ IDE Ruby
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just don't get any bugs then and you'll be fine ;)
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic