Skip to content

Instantly share code, notes, and snippets.

@jling90
Last active May 16, 2017 04:20
Show Gist options
  • Save jling90/a723c37538f2c51069f9b33770f1c07c to your computer and use it in GitHub Desktop.
Save jling90/a723c37538f2c51069f9b33770f1c07c to your computer and use it in GitHub Desktop.
random js thot of the day ft. moment.js
var moment = require('moment')
a = moment("2017-05-16")
b = moment("2017-05-17")
c = moment("2017-05-17")

We should always use moment(a).isBefore(b) instead of a < b. Not because the latter does not work (both forms work!); but rather, we might be tempted to assume something like a == b or a === b behaves similarly.

The following operators do not work on objects and first coerce their arguments to primitives using their arguments' valueOf functions.

+ - * / % & | ^ << >> >>> < <= > >=

Conveniently, moment#valueOf() returns a unix timestamp (as a Number). Notice however == === are absent from the list above. When comparing objects, the valueOf function is always ignored. Instead the object reference will be directly compared.

b < c // false
c < b // false
b == c // false
b === c // false
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment