1

Why does this happen with strings in javascript?

3<=255
true

but

'3'<='255'
false

Is it something to do with the operators or the use of strings?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Ben Muircroft
  • 2,936
  • 8
  • 39
  • 66

3 Answers3

2

I guess it is because it compare ascii values of chars and 3 had greater ascii value than 2. In string it compare char by char if 1 char is false it wont compare else

FireGlider
  • 81
  • 4
0

In first case you are comparing 2 Numbers, in second you are comparing 2 strings. So they are different types and thus produces different results.

coder
  • 658
  • 3
  • 14
  • 31
0

Both.

When the comparison is done on numbers, the values of the numbers determine the outcome.

When the comparison is done on strings, the sort order of the strings determine the outcome.

The string '255' is considered smaller than the string '3', because it would come before it in a sorted list.

Guffa
  • 687,336
  • 108
  • 737
  • 1,005
  • "because it would come before it in a sorted list." --- it's actually the opposite. It would come before in a sorted list because it's smaller. – zerkms Nov 22 '14 at 00:25
  • ... alphabetically sorted list (we're talking about strings, not numbers) – jp-jee Nov 22 '14 at 00:28
  • @jp-jee: `'%' < '&'` <---- strings consists not only of alphabet characters, but are still comparable. Comparability has nothing to do with being a number actually. – zerkms Nov 22 '14 at 00:30
  • @zerkms: No, it's not the opposite, you are just confusing things. There is no concept of one string being smaller than another as they don't have a value that can be compared that way, comparison of strings is done on their lexical order. – Guffa Nov 22 '14 at 00:35
  • @Guffa: there are 2 rules: 1. characters compared one by one 2. characters compared according collation. These 2 rules define the order. These 2 rules also introduce the concept of one string being smaller, or bigger or equal to another. So not sure why you think "there is no concept". Also not sure what "lexical order" has to do with, say, characters `!` and `?`. Which are comparable. – zerkms Nov 22 '14 at 00:38
  • 1
    @zerkms: You are contradicting yourself. You describe how the order between strings are determined, then you say that lexical order has nothing to do with it. If you look up what lexical order means, you will see that it's exactly what you call rule number one, and that it doesn't mean comparing letters as you seem to think. – Guffa Nov 22 '14 at 01:06
  • @Guffa: oh, that's right. I confused the term. But I still not agree that "because it would come before it in a sorted list" is not the other way around. – zerkms Nov 22 '14 at 01:08
  • @zerkms: That's just to describe the comparison in simple terms. If you describe it the other way around then it doesn't say anything about how strings are compared. – Guffa Nov 22 '14 at 01:21
  • @Guffa: well, in JS characters are compared by code points. That's comprehensive and correct (from the ECMAScript point of view). – zerkms Nov 22 '14 at 01:25