Loading
Current section: 2. Variables 4 exercises
solution

Let And Const

Transcript

00:00 So let's go ahead and create our tax rate. So we're going to say const tax rate is 0.08. And you'll notice here first, const is a keyword. In my editor, it's showing up as purple. And then we have a space and then followed by the variable name.

00:18 That space is important by the way. That's I didn't just say that for the fun of it. Then the variable name. Here we have it uppercase tax underscore rate. There is no space between these.

00:29 You cannot do spaces in variable names. There are a handful of characters that are allowed. Interestingly, dollar is allowed in TypeScript. It's actually kind of interesting. When I started doing JavaScript development, there's a popular library called jQuery, and they had a variable name called dollar, simply just dollar.

00:50 And you would say dollar, and then you'd do some whatever thing, and now you can do text. Hello, world. Yeah. Whatever. Setting the text of of some element or something.

00:59 And I was so confused by this dollar thing. Turns out it's just a variable name. So, yes, there are some characters that are allowed to be variable names. Numbers, are not allowed to be variable names unless they're, they are preceded here, so we'd say, preceded by a character. Then you can have numbers in variable names.

01:20 So, couple interesting rules around that, but that's a basic idea. The other thing that you'll notice is that this is upper case, and and it's got that underscore there. The reason for the underscore is because if you didn't have it, then it would be kind of hard to read long names when it's all upper case like this. The casing actually doesn't matter at all. Most TypeScript developers use camel case and you'll see that here in a second.

01:46 There's also Pascal case. This is kind of a screaming snake case, I think is what it's called. A bunch of different conventions for casing, but most of the time you're gonna see me using camel case and we'll see what that looks like here in a sec. And then you have a space. That space does not matter.

02:02 You can actually get rid of that. You can get rid of this one too. The important part here is the equal sign. This is saying assign it to or point it to, this value right here, which we're setting to the number 0.08. So this is less of a reference to an object or something and, which we'll talk about later, and more focused on setting it to a specific value.

02:26 So there is a part in memory that holds this value. Our variable is pointing to that part in mem that spot in memory, depending on the way that this is implemented. So great. There we go. Let's go ahead and create our cart total.

02:41 Now the cart total is something that we want to add and subtract and and everything to. So we're going to assign that to let so that we can change that value. Because this is assigned to const and it's assigned to this primitive value, there's nothing that will ever change about this. Now we'll talk about objects and and things, and I'll give you like a little brief primer in objects for, talking about mutation of the value. But the pointer, that cannot change and that's what const means.

03:10 So let's talk about let. Let allows you to do exactly the same thing. You could even say cart total. There's nothing in JavaScript or TypeScript syntax that says you can't name your variables with the upper case, but convention says that that would be confusing for people who are using your program because typically, upper case means this will never change. But if you use let, then you're communicating, yeah, this actually can and probably will change.

03:37 So we're gonna say cart total equals zero and then we're going to set all of these values. So the book price, and muffin and laptop price listed with a dollar sign. You do not use dollar signs in TypeScript. Well, you do. I literally just said that you can use that in a variable name.

03:56 But as far as, like, denoting money or whatever, TypeScript doesn't have any understanding of what that is as far as the syntax is concerned. So you're gonna say book price, muffin price, and laptop price, and you're just gonna set it to this. I suppose this should probably be $4.50. But the zero on the end here is redundant. It's not necessary and even prettier says, oh, let's get rid of that.

04:20 The formatter says, yeah, this is totally unnecessary. We can get rid of all those. It's the same value anyway. So there we go. We've got our products and now we wanna add the book to the cart.

04:31 So we're gonna update the cart total. There are a couple ways that you can do this. You'll notice we're using this special operator. This is the plus equals and I'll show you this is exactly the same, equals cart total plus. So these two lines do the same thing, where this is just a little bit more terse.

04:52 You're basically saying take the cart total and whatever it is, add to it the book price and then assign that value to the cart total again. So it's just a more terse way of doing this longer thing. So we're gonna do it that way. And then we're gonna add the muffin price, to the cart. So the cart total will plus equal the muffin price.

05:14 And then we'll calculate the final total. We've got Marty the money bag over here helping us in a big way for that, but we want to do a const on the final total because it's final. We're not gonna change it. That's kind of the idea there anyway. And now, I also wanted to have you try to uncomment this.

05:33 And if your editor is set up with TypeScript, which it probably is if you're using a modern editor, then you're gonna see this error, cannot assign tax rate, because it is a constant. So this is the situation where if we had decided to make this a let, it would work just fine. But const protects us from an unexpected reassigning of the variable. There are some developers out there who say just never use const. Just, except for values just like this.

06:02 Like, it's never gonna change and and I really wanna make sure it never changes. I I prefer to use const when the value that I or the variable I'm using, is not going to be reassigned to anything, in general. And so that's why we're using const for all of these values even though these aren't like upper case values or anything. I just prefer to use const as a default behavior. Okay.

06:27 Great. So we're gonna uncomment these all. On my keyboard on Mac, you can do command and then the comment symbol and it will, when you have all those lines selected, and it will uncomment them all. So pro tip. So we get our cart subtotal, our tax, and then the final total.

06:44 And then here we're going to add this export so that the test can grab those values and we can see them right here. So here we go. We've got our cart subtotal as 20.49000002. Tax is 1.6392000002. What is that all about?

07:02 That's kinda confusing. And then our final total is like a little bit more reasonable. So this is kind of interesting. An interesting fact about many programming languages is floating point precision. It's a little bit more advanced topic than I want to dive into in this lesson.

07:20 But basically, you probably shouldn't be using decimals to represent money if you want any sort of precision. When I worked over at PayPal, what we did was, the book price would be $15.99, the number. So 1599¢. And so you're tracking that money in cents. So, just keep that in mind, as you're working with if you ever are working with money in a TypeScript or or actually many other languages program, typically, you're gonna wanna store things in cents, and that way you avoid these precision issues.

08:01 And in fact, here, let's just try that right here. If we, do that tax rate here, just everything is two decimals over. Save this and, now we avoid the problem and we can say, okay, so the cart subtotal is $16.44. The tax is a lot I think I messed up the tax rate. I think this should still be there we go.

08:25 So the tax is this and then the final total is that many cents. And it looks like you got half a cent, so now you're going to have to do some math to round up and all of that. But you can avoid that floating point, precision problem. So we're gonna bring that back just for the fun of it. It's kind of fun.

08:41 People like to joke about the, how JavaScript doesn't do math. JavaScript's not the only one that can't do it. It's just architectural computer things. So, alright. I think that's everything that you need to know so far about variables from this exercise.

08:56 So awesome job, and we'll see you in the next one.