Check out a free preview of the full Deep JavaScript Foundations course:
The "Date to number and ~" Lesson is part of the full, Deep JavaScript Foundations course featured in this preview video. Here's what you'd learn in this lesson:

Kyle discusses Date to number coercion methods including Date.now() static function. He also talks about tilde ~ operator, or otherwise known as "bitwise NOT."

Get Unlimited Access Now

Transcript from the "Date to number and ~" Lesson

[00:00:00]
>> Kyle Simpson: Couple of little tidbits about explicit and maybe implicit depending on how you look at it. Remember I said before that the date object is almost always constructed so that we can then produce that numeric Unix time stamp thing. The Unix [INAUDIBLE] timestamp, okay? Now there's a method you can call on a date instance called get time and it will produce that.

[00:00:20] But who likes to call methods that like tell you what it is you're intending? Let's try to be as clever as we possibly can. It turns out that a Date instance, when coerced to a number, will coerce itself to that timestamp number. So if we coerce a Date instance to a number, like say with the plus operator, will get that time stamp out.

[00:00:41] But you know what, not content, that doesn't show us to be elite enough. We want to be really elite JavaScript programmers. So let's take advantage of yet another little oddity of the JavaScript grammar. It turns out that in JavaScript the constructor form that is where the new is in front of a function call.

[00:01:00] The constructor call form, if there's no arguments to pass, the parentheses are optional. So, like you'll almost always see in open source code for people that really like wanna shave off every single bite from their code, they'll just write new date semicolon. Well, actually, they'll leave off the semicolon, cuz who wants to put all of the unnecessary semicolons in code.

[00:01:18] So you just see new data all by itself as if that's somehow become its own syntactic form for date time stamps or I'm sorry, plus new date. [COUGH] Don't do any of that nonsense, just call date.now. So it's built in since ES5. So every device that you're ever gonna support is already running, just call date.now, it's a static function, it produces that time stamp.

[00:01:43] There's no reason to solve that with Coercion. Now let's turn our attention to this last example here, lines four through eight. So, for all time we've always had this problem of I have a string, have some value and I wanna check to see if there's some value that's contained within it.

[00:02:02] This case, I wanna see if there's a sub-string contained within a larger string. This has been around since the advent of the beginning of programming. We had this back in the C programming days, and when we carried over into JavaScript, we carried over this precedent that if we want to search for a sub-string within a string, we use this index0 function.

[00:02:20] Now, most of the time, when I see indexOf being used, I see people using it specifically to check for the Boolean condition of present or not present. But indexOf is designed to return a number. And so what we end up doing is these silly contortions where we have to check the indexOf to see whether or not it gave us a valid value or not.

[00:02:44] And because this thing is designed only to return numbers, because its precedent comes to us from C where you could only return one value type. So we had to come up with a special value to indicate, did not find it. It's not really an exception cuz it's not an error, it's just it's not there.

[00:03:04] You can't return zero because these are zero based indexes. So what are we gonna return? Well, we chose a special sentinel value, like 500 years ago, the negative one values. It's just a made up special meaning to this number and that number means did not find it. That history comes to us from C and probably even before C.

[00:03:25] I don't even know where it comes from, but it's been handed down from, well, I do it because my dad did it. My dad did it cuz my grandfather did it, right? Nobody even knows why negative one was picked, but it was. So it's the sentinel value that indicates was not found.

[00:03:39] The problem with negative one is that it's a truthy value cuz it's not on the falsy list. So if we wanna compare and say, is this thing true or false, we can't just pass it into an if statement. We, most of the time end up having to say, if indexOf equal equal negative one, or not equal negative one, or some other crap like that.

[00:03:58] So, I'm not a big fan of arbitrary weirdness like that. Stuff that you can't really fully explain, it's just like whenever I teach a brand new JavaScript developer, I'm like I don't know just trust me, it's just that way, I don't know, right? I don't like giving answers like that, I'm a teacher, I like to give the real answer for stuff and many of the times there aren't real answers to these questions.

[00:04:21] It's as arbitrary as some dude just picked negative one, right. Actually, it's probably a woman, because women ruled computer science in the beginning. So probably a really smart woman came up with it, but who knows where it came from that's just what we have now, it's negative one it's an arbitrary sentinel value.

[00:04:39] Now, doesn't make sense when you write it to say equal equal negative one, in this case not equal negative one if we wanna test for the presence of f within that bigger string. Well, I have tested this by teaching brand new JavaScript developers. People that haven't had any programming experience and have never seen the negative one before and I go to explain to them the negative one and they're confused.

[00:05:04] They're like, I don't understand what negative one is about. Why don't you just return false, or null, or NaN, or something else like that? Why does it return negative one? And I think they have a point. Because I think it ought to return NaN. Because NaN is already a sentinel value that establishes invalid, did not find it, does not exist.

[00:05:22] NaN is a very sensible return result for this function but that's not how they designed it. They designed it to return negative one. So what are we to do? Are we to just keep handing down to generation after generation this cult method just compare it to negative one?

[00:05:38] I chose to go a different direction because there's another operator that never gets any use. It's not a terribly useful operator, to be completely honest, but the Tilde operator. That's the ones compliment, which is otherwise known as the binary negate. It works on the bits, it's a bitwise negate.

[00:05:58] [COUGH] It has the mathematical result, equivalent to what we see here. Tilde of some number is the mathematical equivalent of adding one to that and then negating, flipping the sign. What's the only value that you can plug into that equation and produce a falsy value?
>> Kyle Simpson: You all are busting out your algebra skills I see a few of you scratching on paper I'm gonna solve for n, negative one, right?

[00:06:29] It's the only value that I can plug in there. If I plug in negative one and we add one to it and we get a zero and then we negate it, we get the negative zero that's a falsy value, right? So what's interesting about tilde is it prepares that strangely oriented numbering system to answer the Boolean question for us.

[00:06:49] It prepares it to answer the Boolean. Is it existent or not? Now is that magic? Is it intuitive? Of course it's magic unintuitive. Nobody's ever seen the tilde before used like that, of course. But is it anymore magical or unintuitive than negative one? The only reason we know negative one is because everybody's done it.

[00:07:10] So, when choosing between different magic, this is the magic that I happen to prefer just ever so slightly more than the negative one magic. So I tell people to use the tilde, because it prepares it for Boolean coercion the way we would expect it to work. If you don't like it keep using those negative one checks, but I can just tell you brand new developers scratch their head at negative one just like they scratch their head at a tilde, there's not a clear winner here, okay?

[00:07:38] So I put that under the heading of Explicit, it's sort of Explicit and implicit. But I put it in the heading of Explicit because what's it doing specifically is making that number into something that will be treated properly as a Boolean.
>> Kyle Simpson: The real punchline to this is don't use index of it all anymore because as of ES 7, sorry, as of ES 2016, which people call the S seven.

[00:08:04] As of ES 2016, we got array includes and you can use strings as arrays, so now we can just do an includes. And that's the right way to test for this stuff, okay? Cuz that actually returns you a Boolean which is what you want. But if you're stuck using the older way, consider maybe using the tilde instead of your negative one checks.