Today I Learned

hashrocket A Hashrocket project

Interval Comparison in JavaScript

Today I learned a bit about interval comparison. It's something you might recall from grade-school math:

1 < 5 < 10 -- is this true?

Two languages I've found that support it are Python and Clojure:

> if (1 < 5 < 10):
...   True
...
True
> (< 1 5 10)
=> true

JavaScript doesn't support interval comparison, so we have to find another way to check that condition. Here's one technique:

if (1 < number && number < 10) {
  // do something
}

It's a wordier that the previous examples, but I think keeping the variable in the middle of the condition stays true to the interval comparison idea.

See More #javascript TILs
Looking for help? At Hashrocket, our JavaScript experts launch scalable, performant apps on the Web, Android and iOS. Contact us and find out how we can help you.