Introducing var, let, const0:00
Okay, next up, let's figure out the different ways to declare variables. So here you can see our page is importing main.js, and we're all set to go. Now, you already know how to declare a variable using the var keyword, right? But yeah, you're going to start seeing some other things. You'll see let in the wild, and you'll also see const. And this can be kind of confusing at first. And really, it's even more confusing because developers don't yet agree on when to use each of them. So people are kind of divided at the moment. Anyways, let's review them. But first, I'm going to show you an example to illustrate why these new keywords exist. Okay, so imagine you have some kind of fire function, and it's going to accept some kind of Boolean, so that we can control the flow of this function. For example, if the Boolean we provide is truthy,
Var hoisting example0:40
you have some kind of fire function, and it's going to accept some kind of Boolean, so that we can control the flow of this function. For example, if the Boolean we provide is truthy, then I will create a variable, var foo equals var, and then I will log that to the console. All right, so sure enough, if I were to say fire and pass in true, and in this case, I do have liveReload turned on, so you see it instantly here. But yeah, I get var. All right, but what if I were to pass false? And instead, well, maybe we would try to console.log foo anyways. Let's see what happens there. false. Okay, we get undefined. And right off the bat, that may not seem overly weird, but imagine we just log some variable that doesn't exist. Okay, well, in this case, this isn't declared anywhere, so we get a ReferenceError. But foo returns undefined. This is very confusing stuff, and one of those core things you have to
Hoisting explained2:13
So now with this example, it makes more sense that this would return undefined, right? Because it was declared at the top, we just didn't initialize it to anything. So in this case, we set it to var, and console.log would return var. But in this case, it wasn't initialized to anything, so we get undefined. It makes perfect sense once we understand that variables are hoisted to the top. Now, on that note, this is exactly why it's considered a good practice to declare all your variables at the very top. Even, once again, if they're not initialized, declare them at the top, and then you can assign them whenever you need to. Anyways, as you can imagine, over the years, this reality, it's confusing for developers. No two ways about it. And it can have side effects that you didn't anticipate. So there's two new keywords, let and const. Let's first review let. Now,
Using const with arrays4:25
you could default to using let, and that would be fine. No problem there whatsoever. But now you might be thinking, well, what about this const keyword? Does that literally refer to a constant value? For example, a value that never and cannot change? Well, sort of. Unfortunately, it's a little more confusing than that. Let me show you an example. If I were to say const names equals an array, John and Sandy, and if I were to then console.log names, well, yeah, we're going to get the exact same thing. So really, if I were to use var, I'll get it, or let, I'll get it, or const, I'll get it. So when would you use const versus let? Okay, well, let's try this. What if I were to say names equals something else? Maybe Frank and Susan. Okay, well, let's try it one more time. With var, it works. With let, it works. But with const, it does not work. We get a type error. So this could lead you to thinking, yeah, const just
Choosing var vs let vs const6:59
And this is where the community is kind of divided at the moment. A lot of people in a lot of companies have adopted this philosophy. Default to using const. So if I wanted to set foo, I would default to using const like this. The next option is, if you do need to reassign that value and change it later, well in that case you would change it to let. So if I have const name equals Jeff, but later I know I'm going to change it, yeah, with that understanding this would then become let. And then finally, a lot of people would say never use var again. However, yeah, like I said, people disagree. So some people say, no, you can use var. It's good at the top level. So use var at the top level, but still you would use it pretty sparingly. Next, they would say default to using let instead. So basically most of your vars would change into let and then finally use const when you do not want reassignment. So for example, if I had let months
