2

Could someone explain how does JS compare alphanumeric strings? For ex. why the following returns me 'false' when I run it in browser console:

'abc' > 'abc.15' // false

And why this returns 'true':

'abcd' > 'abc.15' // true
SimonD
  • 1,441
  • 2
  • 18
  • 30
  • not sure if we can apply > on strings? – Ram Singh May 03 '17 at 12:39
  • @RamSingh, you can. – Nina Scholz May 03 '17 at 12:39
  • 1
    Yes he can. It does string comparison and `.` is greater than nothing and `d` is > than `.` - also `"10"` is < `"9"` – mplungjan May 03 '17 at 12:39
  • 2
    Try to read ECMAScript spec. It's hard, but it has all the answers if you can get through it. If it's too hard, read Speaking Javascript which is basically spec translated to human language. – dfsq May 03 '17 at 12:42
  • 1
    In this case, [the spec leads here](http://www.ecma-international.org/ecma-262/6.0/#sec-relational-operators-runtime-semantics-evaluation), with the nitty gritty [here](http://www.ecma-international.org/ecma-262/6.0/#sec-abstract-relational-comparison). A good starting point is the MDN page for whatever you're interested in, as they [tend to have links direct to the relevant part of the spec](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Comparison_Operators?v=control#Specifications). – James Thorpe May 03 '17 at 12:44

0 Answers0