Check out a free preview of the full Deep JavaScript Foundations, v3 course

The "Type Check Exercise" Lesson is part of the full, Deep JavaScript Foundations, v3 course featured in this preview video. Here's what you'd learn in this lesson:

Students are instructed to define a new function that implements what was learned in the previous section.

Preview
Close

Transcript from the "Type Check Exercise" Lesson

[00:00:00]
>> Kyle Simpson: To get a better understanding of how primitive values, primitive type values, and some of the special values work, we're gonna jump into this exercise. In this exercise, we're going to make a polyfill. You're gonna define a polyfill for that Object.is utility that we just talked about. So in other words, no cheating, you can't go look at the existing implementation of it.

[00:00:23]
You're gonna write your own Object.is, and in so doing, get some practice with the idea of dealing with these primitive values and their types and the corner cases and things like that. So the instructions here talk about defining an Object.is, it's gotta take two parameters. Remember it takes two values as two separate parameters.

[00:00:44]
And then it's supposed to return true only if they're exactly the same thing. Now normally you would just use a triple equals for that, but the two major corner cases on triple equals where it lies are the NaN value and the negative zero value. So you're gonna essentially need to account for those first, and then if you're not in one of those conditions, then the triple equals would tell you if they are the same value.

[00:01:07]
So the instructions here talk about handling the NaN and the negative zero. And you can use a built-in utility like number.isNaN, but there's also a way of checking for NaNs that isn't that. See if you can figure that out. You can also figure out negative zeros. So one last little note here is that we're defining a polyfill, and the polyfill pattern, as you can see here, is that you only define something if it's not already defined.

[00:01:36]
So that's what a real polyfill would look like, is it would have that if statement wrapped around your definition of your custom function. The challenge here is that you're almost certainly working in a JavaScript environment that already has Object.is, cuz that was ES6, so years back. So if you wrap it in an if statement, none of the code you write is ever gonna run and you're just gonna be testing yourself against the built in Object.is.

[00:02:01]
So as a temporary hack, I would disable that if statement. You can disable it by literally saying something like or true so that you're always defining one. You wouldn't leave that in, obviously, in your normal polyfill. But for the purposes of this exercise, since you're polyfilling something that already exists, make sure that you disable that if statement in some way, or you can just comment out the if statement, okay.

[00:02:26]
But just keep that in mind. You're gonna be working in ex.js. You'll notice that in this file I have a bunch of console log statements, and each one of them is asserting something about this test. And so the objective is that all of these console log statements all have to print true.

[00:02:46]
That is that each one of these calls all return true, and each one of these calls all return false. So you know you have a passing test when you get all trues and no falses printed out. The way to run this code, you can open up this file and run it directly in node from the command line.

[00:03:01]
You could copy your code into a console and run it inside of a console, like in your Chrome developer tools. However you like to run your JavaScript, doesn't need any other surrounding information. But you're gonna define your Object.is polyfill here at the top, and then make sure that it pass all the tests.

[00:03:20]
This would probably take someone between five and eight minutes, so we'll break, let you have some chances to work on the exercise, and then we'll come back and talk about the solution. If you get stock and you need a hint, the solution is provided for you and it's ex.fixed.js in that same directory.

[00:03:38]
All right, have fun with this exercise.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now