This course is out of date and does not reflect Frontend Masters current standards. We now recommend you take the JavaScript: The Hard Parts course.

Check out a free preview of the full Advanced JS Fundamentals to jQuery & Pure DOM Scripting course:
The "Type Checking" Lesson is part of the full, Advanced JS Fundamentals to jQuery & Pure DOM Scripting course featured in this preview video. Here's what you'd learn in this lesson:

Conventional type checking using the typeof operator will return a string for comparison. Similarly the typeof operator can be used to check an object’s prototype. Justin spends most of the time talking about how to type check edge cases.

Get Unlimited Access Now

Transcript from the "Type Checking" Lesson

[00:00:00]
>> [MUSIC]

[00:00:03]
>> Justin Meyer: The next thing we're gonna talk about a little bit is type checking. So it's really useful to know when a something, some variable or some property is of type something else. And how JavaScript provides ways for doing that and what jQuery also helps you with for doing type detection.

[00:00:29] So we talked a little bit already about typeof. Which is great that you can do typeof something that is a number and get back a number. And the instanceof function we learned about that checks if something is an instance of something else. And what you mean by in instance of an JavaScript is really that on its proto chain, somewhere on its proto chain there's an object that is the constructor function's prototype.

[00:01:01] [COUGH] There's other ways that you can kind of check it out to that more or less sometimes work. But the weird thing about JavaScript and this is something that jQuery helps you with, is sometimes you're in a multi-frame environment. Right, you've got a pop up window or you've got an iframe on the page and you want to communicate back and forth.

[00:01:31] The issue that I'll show here, so here I've created an iframe. And I'm getting the iframe's array constructor function and I'm saving it as _Array. And I'm creating a new instance of _Array. If I do typeof arr it gives me object as we know arrays are always type of object.

[00:01:54] But if I do arr as instanceof the normal array I get false. So if you're writing code that you want to be able to work with like arrays from other iframes, it can be very tricky to tell if something is an array or not.
>> Justin Meyer: So, yeah, all these things would fail.

[00:02:19] So what jQuery provides is a way that you'll can always know no matter what host, really what you call these iframes are kind of like host environments. It's like a different, it's almost like the constructor functions in that environment. Those array constructor functions are different than the array constructor function in your main window.

[00:02:38] What jQuery provides is a way to tell if the array is actually arrayed no matter what host environment it came from. So to do that, you can do it yourself by doing Object.prototype.toString.call. This is basically stealing Object.prototype's toString method, but calling it on an array instead of an object.

[00:03:04] And that will always give you [object Array], no matter what host environment this array came from. This is like the true way of checking if something's an array or not. So we're gonna implement our version of $.isArray, jQuery isArray. So this should return true if it's passed a normal array but should also return false if it's arguments.

[00:03:31] But it should also return true if you get an array from a different frame, it should also return true.