Latest / CyberSecurity Summary / You Don't Know JS: Types & Grammar
Transcript
- 0:00Speaker 1: All right, get ready to dive deep into the sometimes confusing,
- 0:04Speaker 1: often surprising world of JavaScript types and grammar. Absolutely with
- 0:09Speaker 1: us as always is expert, and today we were taking
- 0:14Speaker 1: on Kyle Simpson's You Don't Know JS Types and Grammar,
- 0:18Speaker 1: one of best, one of the best books out there,
- 0:20Speaker 1: you know, with the fundamentals absolutely, like what are the
- 0:23Speaker 1: building blocks of JavaScript?
- 0:24Speaker 2: Yeah, so Simpson lays out these seven fundamental types in JavaScript.
- 0:29Speaker 2: They've got null and undefined, boolean, number, string, object, and symbol.
- 0:36Speaker 1: I remember when I first started with JavaScript, undefined and undeclared.
- 0:41Speaker 1: Yoh yeah, really got me tripped up.
- 0:43Speaker 2: It's a common one, for sure. Yeah, a lot of
- 0:46Speaker 2: folks stumble over that. And you know the tricky thing is, yeah,
- 0:50Speaker 2: it's not the variable itself that has the type in JavaScript,
- 0:53Speaker 2: it's the value. Oh okay, So you can have a
- 0:56Speaker 2: variable that exists but hasn't been given a value yet,
- 0:59Speaker 2: so it's undefined.
- 1:00Speaker 1: Okay.
- 1:01Speaker 2: But then you could also have a variable that you're
- 1:03Speaker 2: trying to use that doesn't even exist. It's undeclared. And
- 1:07Speaker 2: the funny or maybe not so funny thing is type
- 1:10Speaker 2: of returns undefined for both.
- 1:12Speaker 1: So it's like JavaScript is saying I don't know what
- 1:14Speaker 1: you're talking about.
- 1:15Speaker 2: Right, exactly, Like I've never even heard of that undefined
- 1:18Speaker 2: Yeah exactly.
- 1:19Speaker 1: So but is that I mean, that seems like a problem.
- 1:22Speaker 2: It might seem like it, but it's actually a really
- 1:25Speaker 2: clever safety net. Oh, it's JavaScript trying to protect you
- 1:28Speaker 2: from yourself by preventing these reference errors that would normally
- 1:33Speaker 2: just crash your program if you try to use a
- 1:35Speaker 2: variable that doesn't exist.
- 1:37Speaker 1: So is this like a featured detection kind.
- 1:41Speaker 2: Of that's a great way to think about it. Yeah, okay,
- 1:43Speaker 2: especially you know back in the day we had to
- 1:45Speaker 2: worry about different browsers supporting different things. Yeah, this was
- 1:49Speaker 2: a lifesaver.
- 1:50Speaker 1: So it's like a like a compatibility checker built in exactly. Okay. Cool,
- 1:55Speaker 1: So we've got these seven types. Let's start maybe with
- 2:00Speaker 1: a RAYS. You know, I used to think of a
- 2:04Speaker 1: RAYS as just like a list of things, but but
- 2:07Speaker 1: I ran into some really weird behavior. Oh with the
- 2:10Speaker 1: length property? Yeah, and delete?
- 2:14Speaker 2: What's with that length and delete? Those are those are
- 2:16Speaker 2: tricky ones. So you can use delete okay to remove
- 2:19Speaker 2: an element from an array. But the weird thing is
- 2:23Speaker 2: it doesn't actually change the length. What it just leaves
- 2:26Speaker 2: this like this gap there. So you end up with
- 2:28Speaker 2: this thing called a sparse array, a sparse array which
- 2:33Speaker 2: behaves differently than like a dense array.
- 2:36Speaker 1: So it's like I delete a word from a sentence,
- 2:38Speaker 1: but I leave the space there.
- 2:40Speaker 2: Yeah, exactly exactly. It looks weird and it can mess
- 2:42Speaker 2: things up if you're not careful. If you need to
- 2:45Speaker 2: actually remove an element and shift everything over, you'd use
- 2:48Speaker 2: something like splice.
- 2:49Speaker 1: Okay.
- 2:49Speaker 2: But yeah, delete and length those are those are different?
- 2:51Speaker 2: Got it?
- 2:51Speaker 1: Okay, So that's on my my weird JavaScript quirks checklist.
- 2:56Speaker 2: It's a growing list, right, it is.
- 2:57Speaker 1: A growing list, it is. Okay. String Okay, I for
- 3:02Speaker 1: the longest time thought that strings were just a raise
- 3:05Speaker 1: of characters.
- 3:06Speaker 2: Yeah, that's a common one.
- 3:08Speaker 1: Is that wrong?
- 3:09Speaker 2: It is? Yeah, so they might seem like that, okay,
- 3:13Speaker 2: But strings are immutable, which means you can't just like
- 3:16Speaker 2: change them in place. Oh okay, So if you use
- 3:18Speaker 2: a string method like the uppercase, it's not actually changing
- 3:23Speaker 2: the original string, it's creating a whole new string really
- 3:27Speaker 2: with the uppercase letters.
- 3:28Speaker 1: Huh. Yeah, that's why my debugger is always so cluttered, right.
- 3:31Speaker 2: It can be hard to track it is. Yeah, but
- 3:34Speaker 2: it's it's actually really good for like, yeah, predictability and
- 3:38Speaker 2: thread safety things like that.
- 3:39Speaker 1: So every time I'm manipulating a string and creating a
- 3:42Speaker 1: whole new string.
- 3:43Speaker 2: You got it.
- 3:44Speaker 1: Okay, that's yeah, I did not know that.
- 3:46Speaker 2: It's one of those things that's often kind of hidden, Yeah,
- 3:48Speaker 2: under the hood.
- 3:50Speaker 1: Cool. Well, let's move on to another one that can
- 3:53Speaker 1: lead to some unexpected behavior. Oh yeah, numbers.
- 3:57Speaker 2: Numbers.
- 3:58Speaker 1: I vividly remember pulling an all nighter in college. Oh no,
- 4:04Speaker 1: because point one plus point two wasn't point three. I
- 4:08Speaker 1: thought my code was broken.
- 4:09Speaker 2: Ah, the classic JavaScript math problem. Yeah, this is uh,
- 4:14Speaker 2: this is where the IE seven fifty four standard it
- 4:18Speaker 2: comes in. It's uh, it governs how JavaScript handles floating
- 4:22Speaker 2: point numbers, Okay, and certain decimal numbers like point one
- 4:27Speaker 2: to point two. They just can't be precisely represented in
- 4:31Speaker 2: binary So it's like trying to fit a square peg
- 4:34Speaker 2: in a round hole.
- 4:35Speaker 1: So wait, so it's not that JavaScript is wrong, it's
- 4:38Speaker 1: it's that's not JavaScript bug. No, it's that computers can't
- 4:41Speaker 1: represent these numbers correctly.
- 4:43Speaker 2: It's a limitation of how computers represent numbers.
- 4:46Speaker 1: Got it?
- 4:46Speaker 2: And it affects a lot of programming languages, not just JavaScript.
- 4:49Speaker 1: So what you're saying is, don't trust JavaScript with my
- 4:53Speaker 1: bank account.
- 4:54Speaker 2: Probably a good rule with them.
- 4:55Speaker 1: Okay, all right, well let's move on then to maybe
- 5:01Speaker 1: two of JavaScript's greatest hits when it comes to confusing terminology.
- 5:06Speaker 1: Oh yeah, Nan and negative zero.
- 5:09Speaker 2: The dynamic duo.
- 5:10Speaker 1: I mean, come on, nan is it's not a number.
- 5:12Speaker 2: Right, But here's the thing. It is a number type.
- 5:14Speaker 1: Wait, wait, hold on, It's not a number, but it's
- 5:17Speaker 1: a number.
- 5:18Speaker 2: It's one of those.
- 5:19Speaker 1: That's like saying a squares in a shape, but it's
- 5:21Speaker 1: also a type of shape.
- 5:22Speaker 2: Yeah, it's It definitely takes a bit of mental gymnastics
- 5:26Speaker 2: to wrap your head around, but think of it as
- 5:29Speaker 2: a signal that's something has gone wrong in a calculation. Okay.
- 5:32Speaker 2: JavaScript uses NAN to basically say, hey, this operation resulted
- 5:39Speaker 2: in an invalid numeric value, and you can't just directly
- 5:44Speaker 2: compare something to NAN with like an equality operation. Yes,
- 5:49Speaker 2: that's why we have number to specifically test for NAN.
- 5:55Speaker 1: Gotcha, Okay? And negative zero, like, why does that even exist?
- 5:58Speaker 1: I've seen it pop up in the console.
- 6:00Speaker 2: I just I know, right, it seems unnecessary at first glance,
- 6:04Speaker 2: but it actually does have implications for certain mathematical operations,
- 6:09Speaker 2: like what like if you divide by negative zero, you
- 6:12Speaker 2: get negative infinity. Oh okay, so it's not just completely useless.
- 6:17Speaker 1: It has meaning.
- 6:18Speaker 2: It does.
- 6:18Speaker 1: Yeah, it's like a secret code. Yeah, kind of.
- 6:22Speaker 2: JavaScript's using to like preserve information.
- 6:25Speaker 1: Exactly about like which direction the value was approaching zero from.
- 6:30Speaker 2: Okay, yeah, that's that's really cool. All right. Well, speaking
- 6:34Speaker 2: of things that can be a little bit misleading, let's
- 6:38Speaker 2: talk about primitive values versus references.
- 6:42Speaker 1: Oh yeah, that's a big one. And this is where
- 6:45Speaker 1: I think, you know, folks coming from other languages especially
- 6:48Speaker 1: like really get thrown for a loop, right, because.
- 6:50Speaker 2: In some languages everything is an object. So in JavaScript
- 6:55Speaker 2: we have this difference between like primitive values like numbers
- 6:58Speaker 2: and strings, yeah, and then objects like a raise and
- 7:02Speaker 2: well objects objects. Yeah.
- 7:04Speaker 1: And the key difference is in how they're stored and assigned.
- 7:08Speaker 1: So primitives, those are immutable, right, so they can't be changed, okay,
- 7:14Speaker 1: and they're always copied by value by value, So when
- 7:18Speaker 1: you assign a primitive value to a variable.
- 7:21Speaker 2: You're creating a whole separate copy of that value. Objects
- 7:26Speaker 2: on the other hand, are different. When you assign an
- 7:28Speaker 2: object to a variable, you're not creating a copy, you're
- 7:33Speaker 2: creating a reference to it.
- 7:34Speaker 1: So it's like two different names for the same house.
- 7:37Speaker 2: That's a great way to think about it. Okay, So
- 7:39Speaker 2: both of those variables are pointing to the same place
- 7:43Speaker 2: in memory, the same object, got it. So if you
- 7:46Speaker 2: modify the object using one reference, the changes are going
- 7:50Speaker 2: to be reflected everywhere.
- 7:51Speaker 1: Right, Yeah, that makes sense. But I can see how
- 7:54Speaker 1: it could go wrong if you're not paying attention.
- 7:56Speaker 2: Yeah, absolutely, you got to really keep that all right. Well,
- 8:00Speaker 2: we're talking about objects. Let's talk about some of those
- 8:03Speaker 2: built in functions that Simpson calls natives men natives like string, number, array, object.
- 8:12Speaker 2: I use these all the time, but I have to.
- 8:15Speaker 1: Admit I've never really understood what's happening behind the scenes.
- 8:20Speaker 1: Like when I use the new keyword.
- 8:21Speaker 2: Oh, the new keyword. Yeah, that's where things get a
- 8:24Speaker 2: little funky. So that's where this concept of boxing comes in.
- 8:28Speaker 2: When you use a native function like string with a
- 8:30Speaker 2: new keyword, you're not actually creating a new string. Oh,
- 8:34Speaker 2: You're creating this thing called an object wrapper, an object
- 8:38Speaker 2: wrapper around the primitive value. So it's like, you know,
- 8:41Speaker 2: you're taking a simple value and you're putting it in
- 8:44Speaker 2: this like fancy box.
- 8:45Speaker 1: It's a decorated string.
- 8:47Speaker 2: Yeah, yeah, exactly. That comes with like extra methods and properties, so.
- 8:52Speaker 1: It's not just a string anymore. It's an object that
- 8:54Speaker 1: contains the string.
- 8:55Speaker 2: That's exactly right. Okay, Yeah, and that object wrapper gives
- 8:59Speaker 2: you extra function Okay. But the really wild thing is
- 9:03Speaker 2: that this boxing happens automatically really when you try to
- 9:07Speaker 2: use methods on primitive values.
- 9:10Speaker 1: Behind the scenes.
- 9:11Speaker 2: Yeah, behind the scenes. So it's like JavaScript is saying, oh,
- 9:13Speaker 2: worry about it.
- 9:14Speaker 1: I got you.
- 9:14Speaker 2: You want to use an object method on a primitive
- 9:19Speaker 2: no problem, I'll just box it for you real quick.
- 9:23Speaker 1: It's like it's like JavaScript magic.
- 9:25Speaker 2: It's a little bit magical. And then there's unboxing.
- 9:28Speaker 1: Oh there's unboxing too.
- 9:29Speaker 2: Yeah, which is just the reverse. Ok So, if you
- 9:31Speaker 2: use an object wrapper in a context where you need
- 9:34Speaker 2: the primitive value, JavaScript will just automatically unbox it.
- 9:38Speaker 1: So take it out of the fancy box exactly. Yeah, Okay,
- 9:42Speaker 1: I never knew about boxing and unboxing.
- 9:44Speaker 2: Yeah, it's it's one of those things that that's cool.
- 9:47Speaker 2: It often happens behind the scenes, all right.
- 9:49Speaker 1: Well, and then there's the array constructor. Yeah, and this
- 9:53Speaker 1: this myth of precizing arrays, Oh.
- 9:56Speaker 2: The precizing myth. Yeah. So I've seen a lot of developers, yeah, right,
- 10:00Speaker 2: to create an array with a specific length, thinking that
- 10:04Speaker 2: they're like being a face reserving memory, pre allocating memory.
- 10:08Speaker 2: But that's not actually what happens when you do that.
- 10:11Speaker 2: You're creating a sparse array with that length. Okay, but
- 10:15Speaker 2: all the slots are.
- 10:16Speaker 1: Empty, so I'm not actually reserving memory.
- 10:18Speaker 2: No, You're not reserving memory, You're just creating.
- 10:20Speaker 1: I'm just I'm just making a list of empty.
- 10:23Speaker 2: Slots exactly like a bunch of placeholders.
- 10:25Speaker 1: Placeholders, Okay, I got it. Yeah, And those can those
- 10:27Speaker 1: can behave strangely, so generally best to avoid that, avoid
- 10:33Speaker 1: pre sizing unless you really know okay, what you're.
- 10:36Speaker 2: Doing, got it, all right, Well, speaking of things that
- 10:39Speaker 2: can be a little bit strange, let's talk about coersion.
- 10:41Speaker 1: Oh, coersion. Yeah, that's a that's a big one.
- 10:44Speaker 2: And I have a love hate it's a love hate
- 10:47Speaker 2: relationship with coercion. It can be really elegant, yeah, and
- 10:51Speaker 2: and and clean, but it can also but by lead
- 10:54Speaker 2: to some tricky buzzs.
- 10:56Speaker 1: Oh yeah, so many bugs. So uh, Coersion, at its
- 11:01Speaker 1: heart is simply jovscript's way of converting a value from
- 11:06Speaker 1: one type to another. Okay, and we've got two flavors
- 11:08Speaker 1: of coercion, right we do, Yeah, explicit and implicit.
- 11:11Speaker 2: Explicit where you're basically, like, you know, explicitly saying JavaScript,
- 11:15Speaker 2: I want you to convert this value. You use you know,
- 11:18Speaker 2: functions like string or number or bullion, and then you
- 11:21Speaker 2: have implicit coercion, which is where it happens like yeah,
- 11:25Speaker 2: as a side effect of operations.
- 11:28Speaker 1: Okay, So give me an example, like what happens if
- 11:30Speaker 1: I add a string and a number together.
- 11:32Speaker 2: Oh yeah, So the plus operator in JavaScript, right, it
- 11:36Speaker 2: could be used for addition or for string concatenation. Oh right, right,
- 11:40Speaker 2: So if one of the operatings is a string, JavaScript's
- 11:43Speaker 2: gonna say, okay, I'm going to coerce the other one
- 11:45Speaker 2: to a string and do string concatenation.
- 11:47Speaker 1: So four plus two.
- 11:49Speaker 2: Would be forty two would be would.
- 11:51Speaker 1: Be a string, forty two a string, not the number.
- 11:53Speaker 2: Six exactly, got it. But if you use an operator
- 11:56Speaker 2: that's strictly for numbers, like say subtraction, JavaScript will try
- 12:01Speaker 2: to coerce any non number operats into numbers.
- 12:04Speaker 1: So forty two, the string and.
- 12:06Speaker 2: Zero would give you the number forty two. I'll give
- 12:09Speaker 2: you the number forty two exactly.
- 12:10Speaker 1: Okay, Yeah, right.
- 12:11Speaker 2: That makes sense. Okay. What about boolean coersion Bullian coersion?
- 12:15Speaker 1: Yeah, this is another big one, right, Yeah, this is
- 12:18Speaker 1: where those truthy and falsey values come in.
- 12:21Speaker 2: I think a lot of people even experience JavaScript developers.
- 12:25Speaker 2: I think this is where oh yeah, things can get
- 12:27Speaker 2: can get weird.
- 12:28Speaker 1: It's easy to trip up on this one, okay for sure.
- 12:31Speaker 1: So JavaScript has this specific set okay of values that
- 12:35Speaker 1: are considered falsey.
- 12:36Speaker 2: Okay, So like false itself, falls itself, null, undefined.
- 12:41Speaker 1: Yep, those are all falsey nan nan yeah.
- 12:44Speaker 2: Zero negative zero, negative zero, and d string empty string,
- 12:48Speaker 2: that's right, okay. And everything else is truth So.
- 12:50Speaker 1: Even an empty object is truthy.
- 12:52Speaker 2: Even an empty object is truthy, which can be a
- 12:56Speaker 2: little counterintuitive. Right.
- 12:57Speaker 1: So if I if I use an empty object and
- 12:59Speaker 1: if statement.
- 13:00Speaker 2: Ye, the code in the if block will execute?
- 13:04Speaker 1: It will it will? Yeah, It's one of those things
- 13:06Speaker 1: you just got to kind of, you know, got to remember,
- 13:08Speaker 1: burn into your.
- 13:08Speaker 2: Brain, burn it in now. Simpson talks about these ye
- 13:12Speaker 2: falsey objects, which yeah, I mean that just sounds like
- 13:16Speaker 2: a paradox to me.
- 13:17Speaker 1: Yeah, that's it's a little bit of a head scratcher
- 13:19Speaker 1: for sure. He's referring to, Okay, some quirks and how
- 13:25Speaker 1: older browsers used to handle certain objects. Like one of
- 13:30Speaker 1: the infamous examples is document in Internet Explorer. So in
- 13:36Speaker 1: older versions of IE, document all was an object that
- 13:39Speaker 1: represented all the elements on the page. But for compatibility reasons,
- 13:44Speaker 1: in later versions of IE, they made it behave like
- 13:47Speaker 1: a falsey value what in boolean context.
- 13:51Speaker 2: So it's like it's an object, but it's not an object.
- 13:53Speaker 2: It's like a.
- 13:55Speaker 1: Quantum object, the JavaScript quantum object.
- 13:58Speaker 2: A JavaScript quantum object.
- 13:59Speaker 1: It's one of those things that you might you might
- 14:01Speaker 1: run into if you're.
- 14:02Speaker 2: Dealing with like really old legacy code.
- 14:05Speaker 1: Okay, so another one for my weird JavaScript quirks checklist,
- 14:09Speaker 1: the long list. Okay, let's talk about explicit okay, bullian coercion.
- 14:15Speaker 2: Explicit bullying coercion.
- 14:16Speaker 1: Now, I know there's the boolean function, but I don't
- 14:19Speaker 1: think I've ever used that.
- 14:20Speaker 2: Yeah, you probably won't see that in a while. Very often,
- 14:24Speaker 2: the more idiomatic way to do explicit bullying coercion is
- 14:28Speaker 2: to use the double negation.
- 14:31Speaker 1: Operator double negation, So I'm saying like not not true, yeah,
- 14:35Speaker 1: which is true, or or not not false which is
- 14:38Speaker 1: false exactly.
- 14:39Speaker 2: Yeah. It's a it's a way to force that boolean context. Yeah,
- 14:44Speaker 2: and it's it's useful when you're dealing with values that
- 14:47Speaker 2: might be either truth or falsey. Yeah, and you just
- 14:50Speaker 2: need to like make sure sure, yeah that you're getting
- 14:53Speaker 2: a bullyan Okay, I got it.
- 14:55Speaker 1: Now, what about implicit bullian coercion implicity especially with the
- 14:59Speaker 1: logical operator and a d.
- 15:01Speaker 2: Oh yeah and the end of the yeah, this is yeah.
- 15:04Speaker 1: Because now these operators it's like they're not just about
- 15:06Speaker 1: logic anymore.
- 15:08Speaker 2: Right, They're not just about logic.
- 15:09Speaker 1: It's like they're yeah, selecting operad yeah, operand selectors.
- 15:13Speaker 2: Yeah, that's a great way to think about it. So
- 15:15Speaker 2: in JavaScript, the and an Indians they evaluate their operands
- 15:20Speaker 2: as booleans, right, right, and then they return one of
- 15:23Speaker 2: the operands okay, based on the results okay, And that's
- 15:26Speaker 2: where that concept of short circuiting comes in.
- 15:28Speaker 1: Right, So with O R, if the first operand is
- 15:32Speaker 1: truth y, yeah, it just it just stops.
- 15:34Speaker 2: It doesn't even look doesn't even look at the second one.
- 15:36Speaker 1: At the second one, I'm done. I found true.
- 15:40Speaker 2: It's like, I'm done here, I got what I need.
- 15:41Speaker 1: That's that's really efficient and with a D.
- 15:45Speaker 2: Yeah, if the first operand is falsey, it's done. It
- 15:48Speaker 2: just stops right.
- 15:49Speaker 1: There and doesn't even look at the second one.
- 15:51Speaker 2: That's right. And that's really important to keep in mind. Yeah,
- 15:54Speaker 2: when you're dealing with expressions that have like side effects,
- 15:59Speaker 2: side effects.
- 16:00Speaker 1: Well, and speaking of side effects, let's talk about the
- 16:03Speaker 1: increment and decrement.
- 16:04Speaker 2: Operator, the plus plus and flip.
- 16:07Speaker 1: These can be tricky. Yeah, those are I know, they
- 16:10Speaker 1: can be tricky. It's like, yeah, it depends whether they
- 16:12Speaker 1: use them before or after.
- 16:13Speaker 2: Yeah, it all comes down to the timing of the
- 16:16Speaker 2: side effect. Okay, So when you use plus plus or
- 16:19Speaker 2: before an operator, that's called pre increment or pre decrement,
- 16:23Speaker 2: and the operator is applied before the value is used
- 16:28Speaker 2: in the expression.
- 16:29Speaker 1: Okay. So so if I have like AA equals five,
- 16:32Speaker 1: and I do plus plus aa becomes six and the
- 16:35Speaker 1: value of the expression is also six, that's right. Okay,
- 16:39Speaker 1: But when I use them after after, that's the that's
- 16:42Speaker 1: post increment.
- 16:43Speaker 2: As post increment or post decrement. Okay, And in that case,
- 16:46Speaker 2: the original value is used in the expression and then
- 16:49Speaker 2: the increment or decrement happens.
- 16:50Speaker 1: So with with A plus plus, the value of the
- 16:53Speaker 1: expression is five, and then A becomes six exactly.
- 16:56Speaker 2: Yeah. So it's, uh, it's subtle, yeah, but it's it's
- 17:02Speaker 2: really important.
- 17:02Speaker 1: It's super important to keep.
- 17:04Speaker 2: Track of yees, especially when you're like in loops or
- 17:06Speaker 2: something like that. The loops a ray manipulation.
- 17:09Speaker 1: Okay, So pre increment modifies before, post increment modifies after.
- 17:13Speaker 1: Got it, I'm writing that down, good idea, Okay.
- 17:16Speaker 2: Now. Simpson also mentions this kind of obscure operator, Oh yeah,
- 17:22Speaker 2: that can be used to like control the order of
- 17:25Speaker 2: these side effects, the comma operator.
- 17:29Speaker 1: A comma operator. I don't think I've ever used that.
- 17:31Speaker 2: Yeah, it's it's one of JavaScript's lesser known features. Okay,
- 17:36Speaker 2: but it can be handy in certain cases. So the
- 17:38Speaker 2: comma operator lets you like string together a bunch of
- 17:41Speaker 2: expressions and it evaluates them from left to right. Okay,
- 17:45Speaker 2: And the important thing is that the completion value of
- 17:47Speaker 2: the whole expression is the value of the last expression.
- 17:50Speaker 1: Okay, So give me an example.
- 17:51Speaker 2: Okay, so let's say you have a plus plus a
- 17:56Speaker 2: comma bay. That comma operator will make sure that A
- 17:59Speaker 2: gets incremented or B is evaluated, and then the value
- 18:03Speaker 2: of the whole expression would be the value of B.
- 18:06Speaker 1: So it's like a way too.
- 18:08Speaker 2: Yeah, it's like a way to control source, yeah, for
- 18:11Speaker 2: that order of execution.
- 18:12Speaker 1: Okay, that's that's interesting.
- 18:14Speaker 2: It's a little comma operator. Yeah. Not something you'll use
- 18:17Speaker 2: every day, right, but good to know about.
- 18:20Speaker 1: Good to know about. Okay, all right, well, let's ship
- 18:23Speaker 1: gears a little bit and talk about building blocks of
- 18:26Speaker 1: JavaScript code. Statements and expressions.
- 18:29Speaker 2: Statements and expressions, Yeah, those are those are important.
- 18:31Speaker 1: Yeah, I have to admit I get them mixed up
- 18:34Speaker 1: sometimes it's easy to do.
- 18:37Speaker 2: So think of statements as like the actions or instructions,
- 18:41Speaker 2: right right, that tell JavaScript what to do. So they
- 18:44Speaker 2: perform tasks like you know, declaring variables, assigning values, controlling
- 18:50Speaker 2: the flow of execution.
- 18:52Speaker 1: Okay.
- 18:52Speaker 2: Expressions, on the other hand, are like values themselves.
- 18:57Speaker 1: So a statement is like a command.
- 18:59Speaker 2: Yeah, a command.
- 19:00Speaker 1: An expression is like a calculation.
- 19:03Speaker 2: That's a great way to think about it. Yeah. So like,
- 19:05Speaker 2: for example, yeah, let a ten that's a statement. Right,
- 19:09Speaker 2: that's an instruction to JavaScript saying, hey, create a variable
- 19:12Speaker 2: called a and assign it the value ten. The expression
- 19:16Speaker 2: ten plus five, on the other hand, that evaluates to fifteen.
- 19:19Speaker 1: Okay, that makes sense. But then Simpson throws in this
- 19:22Speaker 1: idea of statement completion values.
- 19:27Speaker 2: Yeah, the completion value you thought about that. Yeah, this
- 19:29Speaker 2: is a bit of a hidden gem in JavaScript. So
- 19:34Speaker 2: every statement actually has a completion value, really, even if
- 19:38Speaker 2: it's just undefined.
- 19:39Speaker 1: So even like a simple statement like let a ten
- 19:43Speaker 1: has a.
- 19:43Speaker 2: Value, it does. It does. Yeah.
- 19:45Speaker 1: But but if I type that into the console, I
- 19:49Speaker 1: just see undefined.
- 19:51Speaker 2: Yeah, and that's because the console is showing me the
- 19:53Speaker 2: completion value of the statement. So in the case of
- 19:56Speaker 2: let a ten, the completion value is undefined, not ten,
- 20:00Speaker 2: not ten. No, oh, okay, it's it's kind.
- 20:03Speaker 1: Of a it's a behind the scenes yeah, behind the scenes.
- 20:07Speaker 2: Now, Simpson mentions, you can capture these completion values using evil.
- 20:12Speaker 1: Oh evil. Now I've always been told to stay away
- 20:15Speaker 1: from evil.
- 20:15Speaker 2: Yeah, you probably should.
- 20:18Speaker 1: It's a little shady.
- 20:19Speaker 2: It's a little shady. It lets you execute like arbitrary
- 20:23Speaker 2: code from a string.
- 20:25Speaker 1: That sounds dangerous.
- 20:28Speaker 2: It can be. Yeah, So unless you really really know
- 20:31Speaker 2: what you're doing, and you've like really sanitized that string.
- 20:35Speaker 1: Just stay away, avoid at all costs.
- 20:37Speaker 2: Yeah, pretty much. Now there's there's this proposed feature do
- 20:42Speaker 2: expression to do expression. Yeah, that's a that's being considered
- 20:46Speaker 2: for like future versions of JavaScript, and that would give
- 20:50Speaker 2: you cleaner way to capture those completion values.
- 20:54Speaker 1: It's like it's like a supercharged code block, like.
- 20:58Speaker 2: A code block that can return a value, returns of value. Yeah, okay,
- 21:01Speaker 2: that's I'll put that on my things to things to
- 21:04Speaker 2: watch list.
- 21:05Speaker 1: It's still in the proposal state, but something to keep
- 21:09Speaker 1: an eye on, definitely, definitely. Now let's talk about operator precedence.
- 21:14Speaker 2: Operator precedence. Now, I know there's a specific order that
- 21:19Speaker 2: JavaScript evaluates. It follows a hierarchy operators, but I don't
- 21:24Speaker 2: always remember what it is.
- 21:25Speaker 1: It can be, it can be tricky. So it's uh,
- 21:28Speaker 1: it's similar to you know what you might have learned
- 21:32Speaker 1: in math class peendus or.
- 21:35Speaker 2: Bottom parentheses, exponents.
- 21:37Speaker 1: Identification division, editions of traction.
- 21:40Speaker 2: That's the basic idea. Yeah, but JavaScript has a lot
- 21:43Speaker 2: more operators right than just those, So there's like logical operators,
- 21:48Speaker 2: bitwise operators.
- 21:49Speaker 1: So am I going to have to like memorize this
- 21:52Speaker 1: giant Well, luckily.
- 21:53Speaker 2: You don't have to memorize a giant table. The key
- 21:56Speaker 2: is to just like understand the general principles.
- 21:58Speaker 1: Okay, like what.
- 21:59Speaker 2: So for example, like you know, arithmetic operators like multiplication
- 22:04Speaker 2: and division, they have higher precedents okay than like comparison
- 22:09Speaker 2: operators like less then or greater than.
- 22:12Speaker 1: Got it.
- 22:13Speaker 2: Logical operators like A and D and O R they
- 22:15Speaker 2: usually have lower precedents than the arithmetic word.
- 22:18Speaker 1: This is like a pyramid. It is kind of like
- 22:20Speaker 1: a pyramid of precedence.
- 22:22Speaker 2: Yeah, with the with the most important ones at the top.
- 22:25Speaker 2: And if you're ever in doubt, just use parentheses parenthesy
- 22:27Speaker 2: it just be sure purposes. They make it explicit. Got
- 22:30Speaker 2: it all right?
- 22:32Speaker 1: Speaking of things that can be a little tricky, let's
- 22:34Speaker 1: talk about automatic semicolon insertion.
- 22:38Speaker 2: ASI, the great semicolon debate.
- 22:41Speaker 1: Great debate.
- 22:42Speaker 2: Yeah, this is one of those.
- 22:44Speaker 1: It's like a religious war almost.
- 22:46Speaker 2: It can get it can get heated, yeah for sure.
- 22:48Speaker 1: So remind me again, as I what is that?
- 22:52Speaker 2: So a SI is It's basically a mechanism in the
- 22:56Speaker 2: JavaScript engine that tries to fix by inserting semi colons
- 23:02Speaker 2: where they're missing but implied.
- 23:05Speaker 1: So JavaScript is trying to be helpful.
- 23:07Speaker 2: It's trying to be helpful, but sometimes you know, yeah,
- 23:10Speaker 2: it's a little too helpful.
- 23:13Speaker 1: Well, because it's it's making assumptions, right.
- 23:15Speaker 2: It is, yeah about what I meant. Yeah, And those
- 23:17Speaker 2: assumptions don't always line up with reality.
- 23:20Speaker 1: And this is where people start getting getting into fights.
- 23:24Speaker 2: This is where the debate comes in. Right. So some
- 23:25Speaker 2: folks say, look, ASI makes semi colons optional, right, you know,
- 23:29Speaker 2: and it makes your code cleaner, more concise.
- 23:33Speaker 1: Less visual clutter, less visual clutter.
- 23:35Speaker 2: Yeah. Okay, But then you have the folks who have
- 23:37Speaker 2: been bitten by ASI bugs and they say, no, no, no,
- 23:42Speaker 2: always use semi colons, right, you know, to avoid any ambiguity, explicity,
- 23:47Speaker 2: be explicit. Yeah, exactly.
- 23:48Speaker 1: So it's a matter of style versus versus safety.
- 23:53Speaker 2: Style versus safety. Yeah, and you know there are valid
- 23:56Speaker 2: points on both sides.
- 23:58Speaker 1: So what's the verdict semi colons? Are they in or
- 24:01Speaker 1: are they out?
- 24:02Speaker 2: You know, I think the real takeaway is, regardless of
- 24:06Speaker 2: how you feel about semi colons, Okay, you need to
- 24:08Speaker 2: understand how ASI works. Okay, because if you understand how
- 24:11Speaker 2: it works. You can write code, got it that is
- 24:14Speaker 2: both readable and reliable, right, whether you semi Collins or not.
- 24:18Speaker 1: So just know the rules of the game, exactly.
- 24:20Speaker 2: Know the rules of the game, and then you could
- 24:22Speaker 2: play however.
- 24:22Speaker 1: You want, all right, so informed decisions. I like it, Okay. Well.
- 24:26Speaker 1: Simpson also goes into some of the more advanced, potentially
- 24:30Speaker 1: confusing areas of JavaScript grammar, absolutely, starting with the temporal
- 24:36Speaker 1: dead zone the TDZ. Yeah gz gaes.
- 24:39Speaker 2: It sounds like, yeah, like a sci fi movie or something.
- 24:42Speaker 1: Right, but it's it's actually not that complicated, Okay. It's
- 24:46Speaker 1: basically a restriction on when you can access variables that.
- 24:50Speaker 2: Are declared with let and constant.
- 24:52Speaker 1: Right, those those keywords that were introduced in e.
- 24:54Speaker 2: S six for blockscope variables for blockscope.
- 24:57Speaker 1: So unlike var which was functions, gope or global exactly.
- 25:01Speaker 2: Let in constant there, they're only accessible within the block
- 25:04Speaker 2: they're declared in.
- 25:05Speaker 1: Okay.
- 25:06Speaker 2: So the TDZ is basically the period between the start
- 25:10Speaker 2: of the block and the point where the let or
- 25:14Speaker 2: constant variable is actually declared, okay, and during that time
- 25:18Speaker 2: you can't access it. It's like it's in limbo.
- 25:20Speaker 1: So it's like a no fly zone for for variables.
- 25:24Speaker 1: If I try to if I try to access a
- 25:26Speaker 1: variable on the TDZ, my code will crash.
- 25:28Speaker 2: You'll get a reference error. Uh, not quite a crash,
- 25:31Speaker 2: but yeah, it's is JavaScript saying hey, not yet, this
- 25:35Speaker 2: doesn't exist yet.
- 25:35Speaker 1: Okay, respect the PDC.
- 25:37Speaker 2: Got respect the TDC.
- 25:39Speaker 1: And you know it is it actually, I mean it
- 25:41Speaker 1: sounds like a it sounds like a restriction.
- 25:44Speaker 2: It sounds like a restriction, but it's a it's actually
- 25:47Speaker 2: really helpful because it prevents like all sorts of unexpected
- 25:50Speaker 2: behavior and it encourages you to structure your code better.
- 25:53Speaker 1: So it's so it's actually it's another one of these
- 25:56Speaker 1: JavaScript quirks that is actually a lot.
- 25:58Speaker 2: Of those quarks are helpful. Actually, yeah, for our benefit.
- 26:00Speaker 1: Now are there any are there any like Gotcha's. We
- 26:03Speaker 1: should always Gotcha's look out for what the TDZ.
- 26:07Speaker 2: Especially when it comes to like function arguments.
- 26:10Speaker 1: Okay, function argument.
- 26:11Speaker 2: Yeah, so you know in E S six we've got
- 26:13Speaker 2: default parameter values, right, right.
- 26:15Speaker 1: So if you don't pass a value.
- 26:17Speaker 2: If you omit an argument or pass undefined, it gets
- 26:21Speaker 2: that default value.
- 26:22Speaker 1: It's a fallback.
- 26:23Speaker 2: Yeah, it's a fallback. Yeah, Okay, but The catch is
- 26:26Speaker 2: if you try to use that parameter within the default value,
- 26:30Speaker 2: you're going to hit the TDZ.
- 26:32Speaker 1: Wait, so I can't do something like A equals A
- 26:35Speaker 1: plus one.
- 26:36Speaker 2: Yeah, like function foo a equals a plus one. You
- 26:39Speaker 2: can't do that because A is still in the TDZ.
- 26:42Speaker 1: Okay, so no, no self referential the.
- 26:46Speaker 2: Self referential deault values. Yeah, exactly, I got it.
- 26:49Speaker 1: Yeah, Okay, that's that's good to know. All right, Well
- 26:52Speaker 1: we're talking about function arguments. Let's talk about the arguments array.
- 26:58Speaker 2: Oh, the argument's array.
- 26:59Speaker 1: This is an other one that there's another one yet.
- 27:01Speaker 2: Yeah, so the argument's array.
- 27:03Speaker 1: I mean it holds all the argument.
- 27:04Speaker 2: It holds all the arguments yet were.
- 27:06Speaker 1: Passed to a function, even if they.
- 27:08Speaker 2: Weren't even if they weren't.
- 27:09Speaker 1: Like explicitly defined.
- 27:11Speaker 2: Its eplicitely name.
- 27:12Speaker 1: But I've also heard it's it's kind of it's a
- 27:15Speaker 1: legacy thing. Yeah. It has a bit of a mixed reputation.
- 27:18Speaker 2: Yeah for sure.
- 27:19Speaker 1: So first of all, it's not a true array. Oh really,
- 27:22Speaker 1: it's an array like object.
- 27:25Speaker 2: So so they call it the argument's array. Yeah, I know,
- 27:29Speaker 2: but it's not actually an array.
- 27:30Speaker 1: It's not a true array. It's got some array like properties,
- 27:33Speaker 1: like a length property. Okay, but it doesn't have all
- 27:35Speaker 1: the methods of a real array. And here's the real kicker.
- 27:41Speaker 1: In non strict mode, there's this weird.
- 27:46Speaker 2: Linkage between the arguments array, right, and the named parameters
- 27:50Speaker 2: of a function.
- 27:51Speaker 1: So wait, so they're like secretly connected.
- 27:54Speaker 2: Yeah, kind of. So if you modify named parameter inside
- 27:58Speaker 2: a function, corresponding element in arguments gets modified to vice versa. Weird. Yeah,
- 28:06Speaker 2: it's uh, that's one of those things that can lead
- 28:08Speaker 2: to some like really unexpected.
- 28:11Speaker 1: Okay, So arguments are ray use with caution, use with
- 28:14Speaker 1: extreme caution.
- 28:16Speaker 2: If it all, yeah, if it all? There are there
- 28:18Speaker 2: are better ways to handle, you know, variable numbers of
- 28:21Speaker 2: arguments these days, like what like rest parameters which are
- 28:25Speaker 2: part of e S six.
- 28:26Speaker 1: Okay, rest parameters, I'm going to check those out. Put
- 28:28Speaker 1: that on my my to learn list. It seems like
- 28:31Speaker 1: the long list, right, the Java script to learn list
- 28:33Speaker 1: is never ending.
- 28:35Speaker 2: There's always something new.
- 28:36Speaker 1: Well, let's let's talk about another area of JavaScript grammar.
- 28:39Speaker 1: Sure that that can be tricky. The try dot finally statement.
- 28:47Speaker 2: Try finally yes. So this is used for exception.
- 28:51Speaker 1: Handling, right, exception handling right, so try some code, if something.
- 28:54Speaker 2: Goes wrong, if something blows.
- 28:56Speaker 1: Up, and then finally always execute, always executes.
- 29:00Speaker 2: It's like your cleanup.
- 29:01Speaker 1: Crew, the clean up crew. Okay, So, so finally always executes,
- 29:06Speaker 1: whether there's an exception or not. But what happens if
- 29:10Speaker 1: if there's a return statement.
- 29:12Speaker 2: Oh yeah, this is where things the triblock. Yeah, this
- 29:14Speaker 2: is where things get interesting.
- 29:16Speaker 1: Okay.
- 29:16Speaker 2: So if you have a return statement in your triblock, right,
- 29:19Speaker 2: and you also have a return statement in the finally
- 29:22Speaker 2: in the finally blocked okay, the one in the finally
- 29:24Speaker 2: block wins.
- 29:28Speaker 1: Seriously, even if the triblock return to value already, even
- 29:32Speaker 1: if a return to value, yeah, the finally block gets
- 29:35Speaker 1: the final. Say so we have a return in finally, Yeah,
- 29:38Speaker 1: it's going to override everything else.
- 29:39Speaker 2: It's going to override anything that came before. Okay, so uh, you.
- 29:44Speaker 1: Know, be careful what you put in your finally block.
- 29:48Speaker 2: Be very careful what you put in finally.
- 29:50Speaker 1: Especially if there's returns, especially returns. Okay, what about what
- 29:54Speaker 1: about other control.
- 29:55Speaker 2: Flow statements like continue or break?
- 29:58Speaker 1: Yeah, like continue or break?
- 30:00Speaker 2: Yeah? Do they they do have interactions with finally, So
- 30:05Speaker 2: if you have a continue or break inside your tri block,
- 30:09Speaker 2: and you know that triblock is inside say a loop,
- 30:12Speaker 2: right switch statement. Yeah, that finally block is going to
- 30:15Speaker 2: execute before the loop continues or the switch breaks.
- 30:18Speaker 1: So it's like the like the finally interrupts the flow it.
- 30:21Speaker 2: Does of the of the loop it does. Yeah, the
- 30:23Speaker 2: finally block is very persistent. Okay, once it gets code.
- 30:27Speaker 1: Executed, finally always happens, and.
- 30:29Speaker 2: They always happens it. Yeah. Right, Well, Simpson gives this
- 30:33Speaker 2: crazy example of combining finally with.
- 30:37Speaker 1: Label block, label blocks and break statements, and he says.
- 30:41Speaker 2: Is it crazy?
- 30:42Speaker 1: It's crazy. Yeah, maybe we should just uh alone, let
- 30:46Speaker 1: sleeping dogs lie. Okay, let's talk about switch.
- 30:50Speaker 2: Statements, which statements.
- 30:52Speaker 1: Another another control flow statement that I think a lot
- 30:56Speaker 1: of people use but maybe don't don't appreciate.
- 31:01Speaker 2: Yeah, they have their quirks all the all the little
- 31:03Speaker 2: they do. So switch statements are basically a classic way
- 31:07Speaker 2: to like, you know, compare a value. It's a bunch
- 31:10Speaker 2: of different cases.
- 31:11Speaker 1: So it's like a more concise way of like a yeah,
- 31:15Speaker 1: doing a bunch of IFL statement.
- 31:16Speaker 2: More organized way okay to do a bunch of ifls.
- 31:19Speaker 1: All right, but they have some quirks. What do I
- 31:21Speaker 1: need to watch out for.
- 31:21Speaker 2: Okay. So the first thing is the matching that happens
- 31:25Speaker 2: between the switch expression right and the cases uses strict equality.
- 31:30Speaker 2: Oh so the types have to match, so no coercion,
- 31:34Speaker 2: no coercion shenanigans.
- 31:35Speaker 1: Allowed, and switch statements.
- 31:37Speaker 2: Not by default no.
- 31:38Speaker 1: Okay, strict matching. God, what else?
- 31:41Speaker 2: Okay? So another interesting quirk is that case clauses. Yeah,
- 31:47Speaker 2: they kind of fall through to the next one unless
- 31:50Speaker 2: you explicitly use a brake right statement.
- 31:53Speaker 1: Oh I've been bitten by that one, forgotten to break.
- 31:57Speaker 2: Yeah, it's a classic.
- 31:58Speaker 1: And then like weird things have.
- 32:00Speaker 2: Yeah, it can lead to some really unexpected behavior.
- 32:03Speaker 1: Okay. So so strict matching.
- 32:05Speaker 2: Strict matching all through, because all through yep.
- 32:08Speaker 1: Okay, what else?
- 32:09Speaker 2: Okay, here's one that might surprise you. Okay, the default
- 32:12Speaker 2: clause doesn't have to be the last one.
- 32:16Speaker 1: What.
- 32:17Speaker 2: Yeah, it can.
- 32:17Speaker 1: I always thought default had to go at the end.
- 32:19Speaker 2: A lot of people think that, Yeah, but it can
- 32:21Speaker 2: actually appear anywhere.
- 32:23Speaker 1: JavaScript always keep me on my toes full of surprises. Okay, Well,
- 32:27Speaker 1: we've we've talked a lot about like the the internals
- 32:31Speaker 1: of JavaScript, like how the engine sees things, but we
- 32:35Speaker 1: can't forget about the environment.
- 32:37Speaker 2: Oh yeah, the environment is key.
- 32:39Speaker 1: That are code's actually running in.
- 32:42Speaker 2: This is where things get really interesting.
- 32:44Speaker 1: Right, It's like we've been practicing in a nice, safe
- 32:47Speaker 1: environment and now we're going out into the real world
- 32:50Speaker 1: where anything can happen.
- 32:51Speaker 2: That's right.
- 32:52Speaker 1: So where do we even begin with that?
- 32:55Speaker 2: Well, you mentioned ANNEXB earlier ANNXB. Yeah, that's a that's
- 32:59Speaker 2: a good place to start.
- 33:00Speaker 1: I know it's got something to do with how JavaScript
- 33:02Speaker 1: works in browsers, but honestly, it's always been a little
- 33:05Speaker 1: mysterious to me, like what is ANNXB.
- 33:07Speaker 2: So NXB is this uh, really fascinating part of the
- 33:14Speaker 2: ekmascript specification, which is, you know, the official document that
- 33:18Speaker 2: defines the javasup language, And ANXB is basically a collection
- 33:23Speaker 2: of rules describe how JavaScript should behave, specifically in web browsers. Okay,
- 33:31Speaker 2: because you know, over the years, browsers implemented all sorts
- 33:35Speaker 2: of like features and behaviors that weren't actually part of
- 33:38Speaker 2: the standards.
- 33:39Speaker 1: So it's it's JavaScript's collection of like unofficial rules.
- 33:44Speaker 2: That's a good way to put it. Yeah, okay, And
- 33:46Speaker 2: eventually those unofficial rules they became so widespread that they
- 33:50Speaker 2: had to be like officially recognized.
- 33:52Speaker 1: So ANXB is like the rule book for its.
- 33:57Speaker 2: It's like the official acknowledgement all those quirks.
- 34:01Speaker 1: Okay, I like that. Give me some examples. What what
- 34:04Speaker 1: kind of rules are we talking about?
- 34:05Speaker 2: Okay? So one example is regular expressions. Okay, So browsers
- 34:09Speaker 2: have for a long time supported these extensions to the
- 34:13Speaker 2: regular expression syntax that weren't part of the original EKMA
- 34:18Speaker 2: script standard, and but they became so common that they
- 34:23Speaker 2: were eventually like incorporated into ANXP.
- 34:26Speaker 1: So that's like the the reg XP dot one dollar thing.
- 34:30Speaker 2: That's that's ANNXB in action.
- 34:32Speaker 1: I've seen in people's code. That's right, that's that's ANNXB.
- 34:35Speaker 2: Okay. Another example is the extensions to function dot prototype
- 34:40Speaker 2: like arguments and color wait arguments. I know, I know,
- 34:44Speaker 2: I thought that was like it was deprecated. It is
- 34:49Speaker 2: generally discouraged these days, but it's still part of anx
- 34:52Speaker 2: B because it was so widely used, you know, legacy code,
- 34:55Speaker 2: bolder code basis. Yeah, so it's like a it's a
- 34:59Speaker 2: historical artific.
- 35:00Speaker 1: Historical artifact that's still hanging around.
- 35:02Speaker 2: Still hanging around.
- 35:02Speaker 1: So annex B is, yeah, it's like this Museum of
- 35:07Speaker 1: JavaScript quirks.
- 35:08Speaker 2: It's a it's a testament to how JavaScript has evolved, Yeah,
- 35:14Speaker 2: evolved over time.
- 35:14Speaker 1: Okay. I like that analogy a lot.
- 35:17Speaker 2: Yeah.
- 35:17Speaker 1: So ANNEXB that's that's. That's all about how JavaScript works
- 35:21Speaker 1: specifically inm browsers, in browsers, okay, what about subjects? Host objects?
- 35:25Speaker 2: Yeah?
- 35:25Speaker 1: So host objects are another key part of how JavaScript
- 35:30Speaker 1: interacts with its environment. Okay.
- 35:32Speaker 2: And they're objects that are provided, okay, by the host
- 35:36Speaker 2: environment itself. It's not the JavaScript.
- 35:39Speaker 1: Engine in a browser. We're talking about things like the document.
- 35:42Speaker 2: Object, document object, the window object.
- 35:45Speaker 1: The window object, yeah.
- 35:46Speaker 2: All the dom elements, right right, yeah, all that stuff Okay,
- 35:49Speaker 2: that's that's provided by the browser. Okay, and those give
- 35:52Speaker 2: JavaScript the power to manipulate the browser, interact with web pages, right,
- 35:57Speaker 2: respond to events, respond to events, all good stuff.
- 36:00Speaker 1: Okay, but I have a feeling. But here this is
- 36:03Speaker 1: where things get tricky.
- 36:04Speaker 2: Here's the tricky part. Host objects don't always play by
- 36:08Speaker 2: the same rules.
- 36:09Speaker 1: Right, of course not. It wouldn't be JavaScript.
- 36:12Speaker 2: If it was that simple, right, got to have those okay,
- 36:16Speaker 2: So they might have properties that you can't change, even
- 36:18Speaker 2: though they look like regular JavaScript properties and their methods
- 36:24Speaker 2: might behave differently did you'd expect based on the standard
- 36:28Speaker 2: JavaScript object model.
- 36:29Speaker 1: So we have to learn like a whole separate set
- 36:32Speaker 1: of rules.
- 36:34Speaker 2: It's definitely a learning curve, and it's really important to
- 36:38Speaker 2: like consult the browser's documentation.
- 36:41Speaker 1: So when in doubt, check the docs.
- 36:43Speaker 2: Yeah, when in doubt, our TFM R TFM, got it
- 36:46Speaker 2: all right. Now, there's one other topic that I think
- 36:49Speaker 2: we should touch on, which is extending native prototypes.
- 36:55Speaker 1: Extending native prototypes, Yeah, this is this is one where
- 36:59Speaker 1: I feel like there's a lot of a lot of
- 37:03Speaker 1: strong opinions. Oh yeah, So we're talking about.
- 37:05Speaker 2: Adding adding methods to like to like a ray dot
- 37:09Speaker 2: prototype or ray dot prototype, a string dot prototype.
- 37:12Speaker 1: String dot prototype. Yeah, it's tempting, right, right, it seems
- 37:16Speaker 1: so convenient. Just I'll just add.
- 37:18Speaker 2: This, right, just add my add my one method, add
- 37:20Speaker 2: this one little method, right, and then all a rays
- 37:22Speaker 2: can use it.
- 37:23Speaker 1: It'll be available everywhere.
- 37:24Speaker 2: It's so clean, so elegant. Yeah, but then I've heard
- 37:29Speaker 2: it's a bad idea. Yeah, it's it's generally frowned upon,
- 37:33Speaker 2: and for good reason, because the main issue is it
- 37:36Speaker 2: can lead to conflicts and unexpected.
- 37:39Speaker 1: Behavior, especially as the language is.
- 37:41Speaker 2: The language evolves changes. Yeah, because JavaScript is not standing
- 37:44Speaker 2: still right, right, there's always new features coming out and
- 37:47Speaker 2: sometimes like the behavior of existing features changes. So imagine
- 37:52Speaker 2: you add a custom method called I don't know fubar
- 37:57Speaker 2: fubar to a ray dot prototype. Right, and it's great,
- 38:00Speaker 2: your coate is cleaner, you're happy. Everything's good. But then
- 38:04Speaker 2: you know, a new version of JavaScript comes out. Oh
- 38:07Speaker 2: and it introduces a built in fou bar on a race. Okay,
- 38:12Speaker 2: but it behaves differently from your custom one.
- 38:15Speaker 1: That sounds like a nightmare to debug.
- 38:18Speaker 2: It can be a nightmare, yeah, because your custom method
- 38:21Speaker 2: might override the new one or vice versa.
- 38:24Speaker 1: And then yeah, my code's broken and I don't know
- 38:26Speaker 1: why exactly.
- 38:27Speaker 2: Yeah, it can be really hard to track down. So
- 38:29Speaker 2: it's uh, it's best to.
- 38:32Speaker 1: Those treat those native prototypes as sacred.
- 38:34Speaker 2: Yeah, don't mess with them, don't touch. Yeah you can look,
- 38:37Speaker 2: but don't touch.
- 38:38Speaker 1: Got it? Yeah, all right, So no more extending native
- 38:42Speaker 1: prototypes for me. But what if what if I need
- 38:45Speaker 1: to like add some functionality that's missing in older browsers,
- 38:50Speaker 1: Like how do I do that?
- 38:52Speaker 2: Yeah, that's where polyphils and shims come in.
- 38:54Speaker 1: Okay, poly fills and shims. I've heard of these.
- 38:57Speaker 2: Yeah, they're they're really useful, but.
- 38:59Speaker 1: I don't I'm not really sure what they are.
- 39:01Speaker 2: Okay. So a polyphyll is basically a piece of code
- 39:05Speaker 2: that provides a modern JavaScript feature in older environments that
- 39:12Speaker 2: don't support it. Yeah, so I'm raising like filling in
- 39:15Speaker 2: the gaps, hence the name polyphill.
- 39:19Speaker 1: So I'm making my code work consistently across all these
- 39:22Speaker 1: different browsers.
- 39:23Speaker 2: That's exactly right, yeah, okay. And a shim is a
- 39:26Speaker 2: little more specialized.
- 39:27Speaker 1: Okay.
- 39:28Speaker 2: It's a piece of code that modifies existing behavior to
- 39:31Speaker 2: work around like a bug or incompatibility.
- 39:34Speaker 1: So polyphills, polyphils fill in the missing features.
- 39:38Speaker 2: Yeah, fill in the missing stuff.
- 39:39Speaker 1: Shims like smooth over.
- 39:41Speaker 2: Smooth over rough edges.
- 39:42Speaker 1: Yeah, that's a great way to put it.
- 39:44Speaker 2: Okay. And both of those are good, right.
- 39:45Speaker 1: Like those are good.
- 39:46Speaker 2: Yeah, it's good practice to use those.
- 39:48Speaker 1: It's the best practice because they don't, you know, pollute
- 39:52Speaker 1: the global name space potentially conflict with other code.
- 39:57Speaker 2: Okay. Yeah, So polyphills and shims, they are your friends.
- 40:02Speaker 1: There are my friends, okay, a yeah, I'm going to
- 40:04Speaker 1: keep them close.
- 40:05Speaker 2: I'm going to make friends with those.
- 40:06Speaker 1: Yeah.
- 40:07Speaker 2: All right. Well there's there's one other thing that Simpson
- 40:10Speaker 2: mentions about JavaScript in browsers. Yeah, that I think is
- 40:15Speaker 2: worth worth bringing up.
- 40:16Speaker 1: Okay, yeah, so he points out that when we're writing
- 40:20Speaker 1: JavaScript for browsers.
- 40:21Speaker 2: We're actually dealing with multiple, okay, separate JavaScript programs.
- 40:26Speaker 1: Wait, wait, multiple programs. I thought it was just one
- 40:28Speaker 1: big JavaScript program.
- 40:30Speaker 2: It seems that way. Yeah, but it's actually there are subtleties. Okay,
- 40:34Speaker 2: so each script tagh in your HTML represents a separate
- 40:39Speaker 2: JavaScript program.
- 40:40Speaker 1: Oh so it's like each script tag is its own.
- 40:43Speaker 2: Is its own little universe universe. Yeah, and this has
- 40:46Speaker 2: implications for things like, okay, variable scoping, error handling.
- 40:50Speaker 1: Okay, give me an example.
- 40:51Speaker 2: Okay, so, for example, variable hoisting. Oh right, that thing
- 40:56Speaker 2: where variable declarations get kind of conceptually moved to the
- 41:00Speaker 2: top of their scope. That only happens within each individual's
- 41:04Speaker 2: script program.
- 41:06Speaker 1: Oh so it's not across, not across, not across multiple
- 41:09Speaker 1: script tags.
- 41:09Speaker 2: So if I declare a variable in one script tag,
- 41:14Speaker 2: I can't use it in another one, not.
- 41:15Speaker 1: Unless you're using global variables, which is generally a bad idea. Yeah,
- 41:20Speaker 1: globals are bad.
- 41:21Speaker 2: Yeah, keep your scripts nice and self contained.
- 41:24Speaker 1: Okay, so keep it, keep it contained. What about error handling?
- 41:28Speaker 2: Air handling? Yeah, so errors were actually isolated to the
- 41:32Speaker 2: script program.
- 41:33Speaker 1: Or they occur. So if one of my script tags
- 41:36Speaker 1: has an error.
- 41:38Speaker 2: Yeah, it's not necessarily going.
- 41:39Speaker 1: To it won't crash everything.
- 41:42Speaker 2: Yeah, it won't bring down the whole page, It won't
- 41:45Speaker 2: prevent other scripts.
- 41:46Speaker 1: So javascripts got like built in.
- 41:48Speaker 2: Yeah, it's it's designed to be fault tolerance. Fault tolerance. Yeah, Okay,
- 41:52Speaker 2: keep the web running smoothly.
- 41:54Speaker 1: Well, the WIB is a chaotic place, it is. JavaScript
- 41:58Speaker 1: has to be able to handle all of it.
- 42:00Speaker 2: Resilience is key.
- 42:01Speaker 1: Resilience is key. Right, Well, I think we've we've reached
- 42:05Speaker 1: the end of our deep dive here into JavaScript types
- 42:10Speaker 1: and grammar. We did, Yeah, we've really got We've gone
- 42:14Speaker 1: deep dive into into how the language works, uncovered.
- 42:18Speaker 2: Some quirks, some quirks, some gotcha and yeah.
- 42:21Speaker 1: And really seeing how JavaScript has to deal with right
- 42:26Speaker 1: these different Yeah, what are the key takeaways?
- 42:30Speaker 2: I think you know. The main takeaway is JavaScript is
- 42:33Speaker 2: a language that's very.
- 42:36Speaker 1: H deeply connect.
- 42:38Speaker 2: Yeah, intertwined with its environment and understanding that. Okay, it crucial.
- 42:44Speaker 1: It's not just about the rules of the language.
- 42:47Speaker 2: It's not just the language.
- 42:48Speaker 1: Itsel how the language fits into the bigger picture.
- 42:51Speaker 2: The whole ecosystem.
- 42:53Speaker 1: Yeah all right, well, listeners, that's it for our deep
- 42:57Speaker 1: dive into JavaScript types and grammar. Absolutely, we really appreciate
- 43:03Speaker 1: you coming along with us on this.
- 43:05Speaker 2: Yeah.
- 43:05Speaker 1: Sometimes confusing, Yeah, hope, hopefully enlightening enlightened journey. Remember, the
- 43:11Speaker 1: learning never stops. JavaScript is always changing, so stay curious,
- 43:14Speaker 1: keep exploring, and don't be afraid to really dive deep
- 43:18Speaker 1: into the documentation. Until next time, happy coding, everybody,