JavaScript’s Quirks Explained
Over these past few weeks of quarantine, between working on side projects and making sure to enjoy some of the down time I’ve been going over a course I found online called, “JavaScript: Understanding The Weird Parts”. In this course, I’ve discovered so many quirks about JavaScript that bring understanding to anomalies of the language I’ve encountered in my time as a developer.
Many folks tend to run away from understanding why things work this way. Well, I’m here to help offer some explanation, so let’s get to it!
Equality
true == 1; // true
true === 1; // falseWe’ll start simple. In the first example, the loose equality operator == allows for type coercion. The boolean true is coerced to its number representation, which is 1.
In the second example, no coercion takes place… so true is not equal to 1.
NaN is not-a-number, but it’s actually a number?
typeof NaN; // "number"Yep. NaN is a value that represents “not-a-number”, but it’s a property on the Number object which determines NaN’s type.
Weird Math
[] + []; // ""Lets ramp it up a bit. When adding two arrays (or objects in general) they are coerced to their primitive (string) values. Let’s see an example with non-empty arrays.
[1, 2, 3] + [1]; // "1,2,31"
// It's the equivalent of adding "1,2,3" + "1"So for empty arrays, they are first coerced to empty strings. And an empty string plus another empty string equals an empty string.
[] + {}; // "[object Object]"We know from our last example that arrays and objects are coerced to their string representations. For an empty array, it’s an empty string. For the object it’s “[object Object]”. An empty string added to another string will always return the value of the non-empty string, hence our answer.
// {} + [] equals 0
// But, lets change how it looks
{
}
+[]; // 0What’s actually happening is JavaScript interprets the “object” as a code block. It’s an empty code block and an array with the + unary operator preceding it. If you’re not familiar with the + unary operator it coerces the operand to a number. In our case, the empty array’s coerced number value is 0.
true + true + true === 3; // trueBoolean values are implicitly coerced to numbers in addition. true is 1 and false is 0.
Get JoeChre’s stories in your inbox
Join Medium for free to get updates from this writer.
1 + 1 + 1 is indeed, 3.
true - true; // 0Again, implicit coercion.
1–1 is 0.
9 + '1'; // "91"When a string is encountered during addition, it’s no longer a math problem but a string concatenation. So, the 9 is coerced to its string value and added to “1”. And voila… “91”
91 - '1'; // 90Unlike addition, when you use minus sign it is ALWAYS subtraction. It’s always a math problem… every single time. The string “1” is coerced to its number value and… 91–1 is definitely 90.
My Personal Favorites on this list
Let’s show step by step:
(!+[]): the unary operator(+)coerces the empty array into a number, which is 0. The negate operator(!)coerces the value into a boolean and flips its value, so we’re left with the booleantrue.(true + []): the array is coerced to its string value"". What do we know about adding strings? Concatenation.true+""is equal to the string"true".("true" + ![]): The negate operator coerces the value to an boolean and flips the value. The coerced boolean value of an array istrueand it’s then flipped tofalse.("true" + false): Here we are adding a string and boolean value. From what we already know about adding strings, we’re looking at concatenation."true" + falseis equal to"truefalse""truefalse".lengthis equal to 9, and voila.
[] == 0; // trueIn this example, the array is stringified to "" and then coerced to a number. An empty string’s number value is 0 and 0 == 0 is true.
I hope that this allows provides a broad understanding of how JavaScript coerces types. Some of these do seem pretty confusing, but they don’t have to be!







