This course has been updated! We now recommend you take the Deep JavaScript Foundations, v3 course.
Table of Contents
Primitive Types
Introduction
Kyle 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.Additional Resources
Before diving into the course. Kyle provides an array of resources for developers looking to further their JavaScript knowledge. These includes books, documentation sites, and information on using ES6. He also outlines the agenda for the course.Primitive Types
The JavaScript specification contains a number of primitive types. These types include Undefined, String, Number, Boolean, and Object. Primitive types are the bases for all variables declarations.typeof
While some argue that JavaScript does not have variable types, the typeof operator would suggest otherwise. The typeof operator can be used to returns a String representation of a value’s type. Kyle demonstrates the typeof operator and explains it’s sometimes confusing behavior.Special Values: NaN
Within the JavaScript primitive types, there are a number of special values. For example NaN (not a number), Infinity, or null could be used to represent a value. Kyle begins with an demonstration of NaN.Special Values: Negative Zero
Most mathematicians would agree there is no negative zero value. JavaScript would prove otherwise. Kyle demonstrates how you can get a negative zero value and how it may compares to a zero-value in JavaScript. He also talks about a new ES6 method that will check strict equality.Special Values: Quiz
Kyle gives the audience a quick quiz to check their knowledge about special values.Natives
In JavaScript, Natives are functions that will covert a value from one type to another. Kyle explains why he recommends never using the new keyword with Natives. He also gives a few examples of more complex Natives like RegExp and Date.
Coercion
ToString
The ToString function is one of many abstract operations in JavaScript. This function will convert a value to a String representation. While some conversions are obvious, Kyle spends a few minutes demonstrating some of the more peculiar cases with Arrays and Objects.ToNumber
Just 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.ToBoolean
When 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 & Numbers
Explicit 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: Booleans
While 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 & Numbers
Kyle 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: Booleans
I 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 Issues
As 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 Parts
Kyle 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 Parts
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.Double vs. Triple Equal
The 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 Coercion
Using 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 & Surprises
Kyle 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.