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

The "Intentional Coercion" 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:

Kyle explains how to use coercion successfully by having a coding style that makes types obvious.

Preview
Close

Transcript from the "Intentional Coercion" Lesson

[00:00:00]
>> Kyle Simpson: So there are corner cases, we know that that's the reality, there were corner cases. We don't deal with corner cases in any language but least of all, JavaScript, by simply avoiding it. You don't avoid the whole mechanism just to get around a couple of corner cases.
>> Kyle Simpson: You have to adopt a coding style that makes your types and the values that are in those types plain and obvious, that's the only effective way to do this.

[00:00:27]
Some people goes so far as to used whole type systems, and we'll get into that a little bit later like TypeScript and stuff. I don't think you have to go that far but you can adopt a coding style which makes the kinds of types that you're doing much more obvious.

[00:00:40]
I make the claim that a quality JavaScript program is going to have to embrace coercion. You have to make sure that the types involved in every operation are clear. And by the way that means that you shouldn't be designing all these polymorphic functions that can take any sort of value and do 15 different things based upon the value.

[00:01:00]
You're asking for coercion problems when you do that and then papering over them, but I used the triple, I used the triple equals. Why not just design a function that only takes numbers and then make another function that only takes strings? Or make a function that takes numbers and strings, and it's very clear it can only be those two types, and I know what corner cases I have to worry about between numbers and strings.

[00:01:23]
We can choose to be more obvious about how we manage our types, how much we overload, how polymorphic we make our code. We can opt in to more problems or opt out of problems as a result.
>> Kyle Simpson: Biggest takeaway that I want you to get here is that these are fact of your programs and it is within your purview to decide how much or how little you're gonna be affected by those facts.

[00:01:51]
>> Kyle Simpson: That is not to suggest that the only solution to our problem is that everything have to rigidly, everything's completely statically typed. We have to just basically go write Java or C Plus Plus so that we get some kind of like sound type system, that is a reaction to this problem, it is not the the reaction I think that is necessary.

[00:02:12]
So the message I'm preaching here is you can learn these systems, use them appropriately and make them obvious in your code.
>> Kyle Simpson: I do not think that this typing systems are weaknesses, I actually think this is one of JavaScript's strongest qualities. It's an unsung hero, I think it's one of the reasons why JavaScript is the ubiquitous language that it is today because it has been so palatable to so many different use cases.

[00:02:40]
The first truly multi-paradigm language and a big reason why it has been able to survive multi-paradigm is because of its type system. And that's completely opposite from what every one of my peers and everything else that you've ever heard. Everybody else has always told you this is the worst part of JavaScript and I'm gonna put my foot down and say it's one of the best parts of JavaScript.

[00:03:04]
And if you're not using it, your programs are missing out

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