Debugging and Fixing Common JavaScript Errors

# Common JavaScript Quirks 2

## Todd Gardner

TrackJS

### Check out a free preview of the full Debugging and Fixing Common JavaScript Errors course

The "Common JavaScript Quirks 2" Lesson is part of the full, Debugging and Fixing Common JavaScript Errors course featured in this preview video. Here's what you'd learn in this lesson:

Todd pulls another volunteer from the audience to review JavaScript quirks.

Preview
Close
Get \$100 Off
Get \$100 Off!

### Transcript from the "Common JavaScript Quirks 2" Lesson

[00:00:00]
>> So let's play Who Wants to be a JavaScriptaire Part 2? Who'd like to be our contestant this afternoon? Do I have a volunteer? Are you ready to play Who-
>> Wants to be a JavaScriptaire? Same rules as before, you get one lifeline. All right, what will this JavaScript code return?

[00:00:20]
Math.max called without any argument, will it be A Infinity, B -Infinity, C NaN, or D 10e25678?
>> I don't know, I'm thinking infinity would be my first go to, but without an argument, I'll go with C.
>> C, not a number, final answer?
>> Final.
>> Unfortunately, as is typical for JavaScript, it's not what you would expect at all, called with no arguments.

[00:00:58]
It thinks the difference between two not defined arguments, which is different than passing in undefined twice, will return -Infinity.
>> Darn.
>> All right, that's all right. Question 2, what will this JavaScript return? What is the length of a function? Is it A, undefined? Is it B 0, C 2, or D 21?

[00:01:28]
So we're live on television. What are you thinking?
>> I don't know.
>> What would the length of a function be?
>> I'm thinking C.
>> C, why do you think it's C?
>> From a function in general, I'm not quite sure. It's just kind of a random guess, let's pop-

[00:01:50]
>> Random guesses are good. I like random guesses.
>> I don't know. I know it's an object, of course, just like everything in JavaScript is, for the most part. I don't know, I'll just go with C again
>> C, 2?
>> Yeah.
>> Final.
>> All right, you are correct.

[00:02:15]
The length of a function in JavaScript specifies the number of arguments defined in its definition.
>> Sweet.
>> Not the number of arguments passed or arguments used, but the arguments specified, so A and B.
>> Nice.
>> What will this JavaScript code return, +"42"? Will it be A 42, B 43, C "43", or D NaN?

[00:02:46]
And remember, you do still have a lifeline.
>> This one feeling a little bit more confident with but not entirely. I know it's definitely not a number, so D, that's probably the obvious answer. I don't think it's gonna return A or B either because those are numbers, so, So it's either gonna be C or D.

[00:03:23]
>> All right, audience.
>> C or D.
>> So by round of applause, who thinks it's A? Who thinks it's B? Who thinks it's C?
>> [APPLAUSE]
>> Smattering, and who thinks it's D?
>> [APPLAUSE]
>> Different smattering. I'm not sure that that helped you a lot, sir.

[00:03:47]
[LAUGH]
>> Well, at least I know that I'm confident with C or D though.
>> [LAUGH]
>> The audience certainly agrees with you.
>> We'll go with C.
>> It's A.
>> I think it's C.
>> All right, "43". Unfortunately, it is the number 42 because the plus operator is not preceded by a string.

[00:04:14]
And so it is an arithmetic operator, and therefore, coerces the value into a number. And so this gets evaluated as 0 + 42 or 42. All right, next question, what will this JavaScript code return? Poop emoji minus poop emoji? Will it be, A ' ', B the poop emoji, C, <Error>, or D NaN?

[00:04:45]
>> Hmm, we know it's equals to in Unicode, right, for a dot length?
>> Well, yeah, as we learned before, the length of each of these strings is two.
>> Okay. I don't know if you can subtract them though. So I'm thinking it's gonna be either C or D.

[00:05:17]
I will go with C.
>> C, <Error>?
>> That's right.
>> All right, let's see if you're right. Unfortunately, it is NaN. Very few things in JavaScript actually throw errors. The minus operator is always arithmetic. And so it will attempts to coerce both sides into numbers, which it cannot because a poop emoji does not have a corresponding number value.

[00:05:43]
And so therefore, two not a number, subtracted from each other continues to be not a number. What will this JavaScript code return, !!""? Will it be, A "", B <Error>, C true, or D false?
>> I don't know, well, we know very few things throw errors.
>> Very few things in JavaScript will throw an error.

[00:06:17]
>> And-
>> It's very tolerant of whatever garbage you want to throw at it.
>> So we'll skip that one for now. I'm kinda feeling keep going with C here, but I'm thinking it's gonna be true for whatever reason because,
>> Why do you think it's gonna be true?

[00:06:46]
You think it's gonna be a Boolean, I guess.
>> Well, yep, because it's empty so empty string, I think, does return false, am I right?
>> Yep.
>> Okay. Yeah, so let's go with-
>> C true.
>> I don't know the argument explanation-
>> Well, unfortunately, I have a different answer on the next slide.

[00:07:13]
So I'm sorry, that is incorrect, it is false. The not, not syntax flips the value twice. So the first not changes the empty string into its opposite Boolean value, so the opposite value would be true, and then the second not flips it back. So the not not anything trick is using JavaScript as a shortcode to coerce a value into its Boolean type.

[00:07:39]
>> A lot of got yous.
>> As a totally unrelated thing from a question that did not make the quiz here, 1/0 in JavaScript is infinity.

### Learn Straight from the Experts Who Shape the Modern Web

• In-depth Courses