Check out a free preview of the full Getting Started with JavaScript, v2 course

The "Types Summary" Lesson is part of the full, Getting Started with JavaScript, v2 course featured in this preview video. Here's what you'd learn in this lesson:

Kyle summarizes the section on types by advocating for critical thinking around how to use types in a program, when to allow type coercion, and when to disallow it.


Transcript from the "Types Summary" Lesson

>> Kyle Simpson: So the point is that, like every other operation, the real question you should be asking yourself is, is the coercion helpful? Is it gonna be useful to me at this point or not? It puts the onus on you. As you're learning to be a better developer, you should be making critical thinking decisions for yourself.

Rather than relying upon what somebody wrote in a tweet or a book somewhere, someday, ten years ago, you should really be asking yourself, for this particular situation, is it gonna be helpful to me or not?
>> Kyle Simpson: My claim, my thought is that the double equals is not really about comparisons with unknown types.

That's sort of the the fear that is often spread about this topic, is that the double equals is, you have these unknown types and have all these weird corner cases and stuff. That's not really what double equals is about because the more important thing, the thing I want you to take away from this discussion, is that you should be making your types obvious when you make your comparisons.

Make it clear that you've got a number and a number. And if you know the types, then double equals is entirely safe. You can use it in places where you know the types, and then you can optionally decide to use it in places where you wanna allow the coercion.

For example, that null and undefined case that I just listed.
>> Kyle Simpson: JavaScript has a dynamic type system, and it uses various forms of coercion both implicit and explicit to handle those value type conversions, including in our equality. This is a good thing. You simply cannot write quality JavaScript programs without embracing that true fact, without knowing what the types are and making sure that it's obvious in the code, what types are involved in those operations.

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