Transcript from the "var vs let" Lesson
[00:00:48] And let and const are newer types of declaring variables, newer keywords let's say for declaring variables, that introduce a new scope restriction around when those variables are gonna be in play, compared to var. So var should be something that we're comfortable reading because we're gonna still find it a lot in examples, in code that we find around the Web, other people's code, that sort of thing.
[00:01:19] But we're not going to have much reason to wanna declare new variables in our code using var. This is to say when it comes to mutability, var variables are re-assignable just like let variables. So newVariable = goodbye, is totally valid in var variables the same as it would be with let.
[00:01:50] Now, when it comes to functions we said that like for example let variables because we can reassign them we can mess with them inside of our functions. So, function mess with, we can have now setting, new variable to messed. So, now if I call and just to check, so new variable once again was goodbye.
[00:03:37] So let's say I'm gonna do a letvariable = original value and then within some curly braces I'm gonna do let letvariable = new, Let behaves differently. So, what happened here is that inside of the curly braces I created a new scope as far as let is concerned, but not as far as var is concerned.
[00:04:12] So that is one difference between var and let. And const also has what is called block scoping like let does. But because we're not usually reassigning const variables where we can't ever reassign const variables, it doesn't trip us up as much. But with let because we can reassign, let, within a block scope and get a new variable, it's essentially, let's go back to our whiteboard, it's essentially the same thing as in our planets example, were we had a global, what did we call it?
[00:04:57] Let variable that pointed to some string original, I think we called it, And then when we created our curly braces that gave us a new scope, that's called a block scope that doesn't have a name, or anything, just it was curly braces, and in there we created a different let variable, which pointed to something else, The same way that we had our planets in our function scope before.
[00:06:08] Instead what we did is we looked up the old var variable in the outer scope. We gave it a new arrow Pointing to a new value. And we actually change the value of the variable in the global scope. So with respect to functions var and let work pretty similarly in terms of they both behave with the new function scope.
[00:06:34] But with respect to block scope in the curly braces, which is gonna come up a little bit later when we have things like loops, for example, or IF statements, which we're gonna look at later, in those cases the behavior of let and var variables is different, they have different scoping rules.
[00:07:16] And the chances of, oops, wait, what do you mean it's actually didn't declare a new variable inside the block it actually used the old variable. And, no, now, my value has changed I didn't expect it to change, that chances of that happening are way higher in my experience.