Deep JavaScript Foundations, v3

Corner Cases: Summary

Kyle Simpson

Kyle Simpson

You Don't Know JS
Deep JavaScript Foundations, v3

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

The "Corner Cases: Summary" 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 offers advice on using double equals for coercive equality comparisons.

Preview
Close

Transcript from the "Corner Cases: Summary" Lesson

[00:00:00]
>> Kyle Simpson: A takeaway, or a way to summarize, how to avoid these corner cases with the double equals. These are a set of guidelines that I think you can use. You can use double equals, but avoid it in these particular scenarios. Number one, avoid the double equals when either side of them can be a 0, or an empty string, or even one of those strings with only whitespace in it.

[00:00:21]
Don't use double equals in those cases. There's just a lot of corner cases that you might get bitten by. Some people say well, I can't, no. Okay, but is there anyway for you to restructure your codes so you can? Cuz I think that's better. It's at least a better code, regardless of whether you like double equal or not.

[00:00:42]
But let's say you can't. Well, then another check that you should ask or to avoid the double equals,
>> Kyle Simpson: Don't use it with non-primitives. I don't even use it with the identity comparison on them, even though I know it works. But just to be super safe, I don't use it with primitives at all.

[00:01:01]
So if I was really trying to check is this reference to the array the same as this reference to the array, I'm not gonna use the double equals. Even though it would work in that one specific center, it's close enough to the edge that it's not worth it.

[00:01:12]
That's my takeaway, or my preference, okay? So don't use the double equals when they're non-primitives, only use it for coercion among the primitives. And definitely don't use double equals to true or double equals to false. Essentially, just allow the ToBoolean to happen, which is the better outcome anyway.

[00:01:31]
Or if you really can't allow that, if it really has to exactly true or exactly false, which sometimes it does, then use triple equals. This is a relatively short list. And I would make the case that any developer that cared to learn about these systems could learn this list and effectively do so, effectively communicate in their code that they're avoiding these problems.

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