2

I found some code from a departed colleague and no one is sure what the plus in front of the following Date objects is doing.

return {
  isActive: +new Date(notice.reportedAt) === +this.activeNoticeReportedAt,
  ...
}

I feel like it is some kind of JavaScript trick that is supposed to protect against undefined, but that is just a wild guess.

Please not that activeNoticeReportedAt is already a Date

JDurstberger
  • 4,127
  • 8
  • 31
  • 68

1 Answers1

4

It turns it into a number, which represents the date's Unix timestamp in milliseconds:

> +new Date()
< 1542726854220

The reason for using it here is that Date objects can't be compared using the == operator, because that only checks for object equality, not value equality:

> x = new Date("2017-01-01")
> y = new Date("2017-01-01")
> x == y
false
Thomas
  • 174,939
  • 50
  • 355
  • 478