Table of Contents
IntroductionKyle Simpson begins the course with a brief overview of what he’ll be covering throughout the course. He mentions a number of open source projects he maintains and the book series he is currently writing.
Special Values: QuizKyle gives the audience a quick quiz to check their knowledge about special values.
ToNumberJust like the ToString function, the ToNumber function will convert one value to another. In this case, the conversion is to a number. While number conversions are fairly straight forward, Kyle covers some issues that can lead to larger pitfalls. He also drills a little deeper into how the toNumber coercion is performed behind the scenes.
ToBooleanWhen determining if something is false, the ToBoolean function references a list of “falsy” values. If the object in question is on that list, the result is false. If it isn’t on the list, the result is true. K
Implicit vs. Explicit Coercion
Explicit Coercion: Strings & NumbersExplicit coercion happens when it’s obvious from the code that one type is being converted to another. Kyle shares a few code examples demonstrating ways to explicitly coerce values from String to Numbers and vice versa .
Explicit Coercion: BooleansWhile using the Boolean() native function is Kyle’s preference, many developers will use a double negate (!!) to perform Boolean coercion. Kyle explains how this works and demonstrates a few other types of explicit coercion.
Implicit Coercion: Strings & NumbersKyle defines implicit coercion as a side effect of some other operation. In other words, it’s not clear when looking at the code that coercion will occur. Kyle introduces implicit coercion with a few examples using String and Number values.
Implicit Coercion: BooleansI most cases, Boolean implicit coercion occurs when performing conditional logic. For example, using a Number or String variable inside an if statement. Kyle talks about Boolean implicit coercion and warns the audience about using a double-equal operator in a boolean comparison.
Double-Equal IssuesAs Kyle mentioned earlier, using a double-equal operator in a Boolean comparison can lead to issues. In some cases a Boolean conversion will occur. In other cases it will not. These inconsistencies are why Kyle recommends avoiding this usage.
Implicit Coercion: The Bad PartsKyle walks through the worst possible offenders with implicit coercion. These are the cases where the result of the coercion is completely unexpected. He leaves the audience with a few practical takeaways and best practices.
Implicit Coercion: The Safe PartsWhile 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.
Double vs. Triple EqualThe double-equal operator allows coercion in a comparison. The triple equal operator does not. Kyle walks through number of example demonstrating how the result of these two operators can differ.
Helpful Implicit CoercionUsing the double-equal operator can be helpful. Kyle shares some pro-tips about how to determine when this operator should be use. He also talks about the performance impacts of coercion.
Coercion Resources & SurprisesKyle wraps up his coverage on coercion with a some helpful resources. He also talks a through a few last surprises he has discovered during his research on coercion.