While many developers believe implicit coercion to be evil, Kyle shares the opposite view. He gives a number of examples why it can be beneficial and leads to simplified code.
(upbeat music)…- [Kyle] Here's some of the safe parts,…the things I'm talking about.…These are all ones that happen very often in our programs.…We know that numbers are always positive, greater than zero,…we know that strings are always nonempty.…If you have a part of your program that is like that,…and you know that it's reliable in that way,…don't worry about it.…Just let that code work with a double equal.…You don't need the triple equals there.…Now, if you want to--…I mean, if I'm being almost a little bit harsh about it,…if you want to be lazy, the lazy way out…is to always use triple equals.…
Because then you don't really need…to think about it too much.…Except there will be places…that you're gonna have to think about it…more than you should and those are the places…that we're headed towards for me to show you…why I think double equals is useful.…Alright, so one of those I've already kind of touched on.…It's this idea that primitives and natives…automatically coerce between each other.…That's a really useful part of implicit coercion,…
Note: This course was created by Frontend Masters. It was originally released on 8/29/2015. We're pleased to host this training in our library.
- Primitive types: undefined, string, number, boolean, and object
- Special values: NaN and negative zero
- Natives: Regex and date
- Functions: toString, toNumber, and toBoolean
- Implicit coercion
- Explicit coercion
- Strings, numbers, and booleans
- Operators: Double equal and triple equal