Skip to content Skip to sidebar Skip to footer

Is There A Total Order On Javascript Objects?

The title pretty much says it all. Does JavaScript guarantee a total order on objects when using the <, >, <= and >= operators? I wrote some code just to check total or

Solution 1:

Depends on what objects we're considering. If we confine our attention to the class of numbers, then yes, the order will be total. As your example demonstrates, the same can be said of (at least some) strings. But totality doesn't seem to hold generally.

For instance, if you add var h = 5; to your declarations and then add h to your thereIsTotalOrder call, you will get a false. That's because in a state where h = 5 and c = 'foo', (h &leq; cc &leq; h) is false (which means that totality is not satisfied).

As you rightly noted, while the absence of a false value returned by thereIsTotalOrder doesn't prove that all objects are totally ordered, the presence of a false value does prove that the order (if defined) between all objects is not total.

Post a Comment for "Is There A Total Order On Javascript Objects?"