If you’re interested in exploring these quirks further, here is a resource that includes a quiz, which covers many of them at jsisweird.com.
===). The triple equals compares the value and make sure that both items are of the same type. This fixes the problem.
There is also a not double equals (
!==), which is the not equals of the triple equals world.
00:52 The weirdness goes deeper than just strings and numbers. You can get some strange things with arrays as well.
01:03 See? Kind of weird. Best practice is always use triple equals.
BigInt type. Fortunately, it was adopted quickly and as of this recording has almost 90% penetration in the browser space.
If you’re dealing with large numbers, you may get unexpected behavior. The introduction of
n to indicate that a number is a primitive type
BigInt is much larger than the biggest integer for a number. The ceiling problem from before has been lifted. Without the
'bigint'. And of course, there’s also the
BigInt object, which will always be
'object' because it’s a reference type.
The parameters in the declaration are a shortcut to make it easier to get at what was passed in, but they don’t change the execution. They’re there to make your life easier, that’s it. There’s a neat feature, which I haven’t shown you, though, which is the
arguments variable, which you can get at to see all of the arguments passed in.
03:23 Let me open up a Node session and demonstrate all of these things. First, let me define a function.
!), or bang, is the NOT operator. Using it twice like this is a shortcut to convert a value to Boolean.
03:55 The first time you NOT something will convert it to Boolean and negate it. The second time you NOT something will invert it. And of course, if you’re inverting the negated thing, you get a Boolean of the same value as the thing passed in. Now let me call it.
1 to be equivalent to
True. That’s great and everything, but I believe I was talking about arguments.
Let’s send all sorts of random stuff to
42, which is a number greater than
0, which it considers
True, and then ignores all the other arguments.
and you get
False back. Same explanation as before, except this time I’m getting the truthiness of
0, which is
Calling the function without arguments will result in an
undefined converts to
The contents of
arguments is an array-like thing containing the arguments to the function. Using the triple dot rest operator (
...) destructures arguments inside of the array.
I can then run the
.reduce() function on the resulting array and sum up its contents.
Look at that! Math. You may have heard me use the phrase “array-like” when I spoke of the
arguments value. I did that on purpose. It isn’t an array. It is iterable, and as you’ve seen here, it can be restructured. But unlike an
Array object, it doesn’t support
.reduce() or any of the other
This is why it had to be destructured into an array before calling
.reduce() in the
sum() function. I guess that’s a bit of a quirk inside of a quirk.
06:01 It’s moving past quirk-tastic and into quirk-errific territory here.
Variables that have been defined but not set to a value are set to
undefined. You can assign
undefined to a variable or you can also assign
null to a variable.
null. If you send
undefined instead, you’d have no way of distinguishing between that case and the argument being left off altogether. With default arguments available in ES6, the importance of
null is waning.
Next up, a quirk so complicated it gets its own lesson: the calling context of functions and
Become a Member to join the conversation.