JS Built-ins
StringString.prototype.localeComparebeginner

String.prototype.localeCompare() β€” less-than returns negative

πŸ“– Usage Example
'apple'.localeCompare('banana')   // β†’ negative
'banana'.localeCompare('apple')   // β†’ positive
'abc'.localeCompare('abc')        // β†’ 0

Use localeCompare() to compare strings in a locale-aware way, returning negative, zero, or positive.

String.prototype.localeCompare() β€” Less Than

When str sorts before compareString, the result is negative.

Challenge: Implement comesBefore(a, b) that returns true when a sorts before b.

comesBefore('a', 'b') // β†’ true
comesBefore('b', 'a') // β†’ false
πŸ’‘ Hints (1)
  • β€’ A negative return value means the first string sorts before the second.