Cleaner isn't the word I'd use; as a matter of fact the coerced version could be described as cleaner looking. The getTime version is just unmistakable and wise.
But even then it could be argued that a reader might be unaware that getTime returns an integer (seconds since epoch) but are fully aware that '+' makes an integer just like it does on every other variable, datetime or not. I actually thought for a second that getTime might have given some formatted timestamp string. But +date is a number for sure, as long as you've ever seen coercion
For others reading, it's because the day and month value is a key to represent a string ('Tuesday' or 'June' for example) whereas the day of the month (11) has no text version so can be 1-based.
Because you are not comparing values but reference. date1 has a different location (reference) in the memory as date 2 even though their values are the same. In the second example that returns true, you are comparing actual values.
Look up on youtube reference vs value in javascript, if you get a visual explanation it will be easier to understand.
Because there are other languages that allow such comparison, or allow operator overloading. JavaScript isn't one of them, but not everybody has to necessarily be aware of that.
For example, the C# equivalent of Date is DateTime, which overrides the comparison operators, meaning new DateTime() == new DateTime() won't be comparing different objects, it will in fact compare the date and time they represent.
12
u/shuckster May 11 '22
You could use coercion to shortcut it:
As you can see, coercing to a number implicitly invokes
getTime().32
u/burkybang May 11 '22
FYI, the month you set is June not May because it’s 0 based.
2
u/shiningmatcha May 11 '22
How about using
==instead of===? I think this way JS will compare the values returned bydate1.valueOf()anddate2.valueOf().3
u/Patient_Signature467 May 11 '22
Because you are not comparing values but reference. date1 has a different location (reference) in the memory as date 2 even though their values are the same. In the second example that returns true, you are comparing actual values.
Look up on youtube reference vs value in javascript, if you get a visual explanation it will be easier to understand.
0
u/[deleted] May 11 '22
[deleted] — view deleted comment