Welcome back to Section three, lesson five, declaration and assignment today we're going to be talking
about variable declaration and assignment and how we use variables in JavaScript.
Variable declaration is simply using varlet or const these special function keywords that we use to
denote that we're initializing a variable and giving that variable a name.
So in this situation, we would use either const Aulet or VAR and I can talk about the differences of
those and then you just give your variable a name your variable cannot start with.
Remember, it cannot start with a number.
There are a few other rules, but for the most part you want to give your variable a name that is descriptive
for what you're trying to accomplish.
If you want the value of this to be something related to a car, car would be a very good name for your
variable.
If you wanted to have some number that you're probably going to change using some other code, then
Count is a great name for that variable and you can make these whatever you want.
Variable assignment is when you use the assignment operator, which is the equals sign to give the variable
a value.
So you're going to start with some value.
Use the assignment operator and then your variable name, and that way they'll be tied together, variable
assignment can be described either as putting this data into a box with this label or alternately thinking
of the variable as a shortcut.
Like on a computer, you can set a shortcut to open a file.
The variable is a shortcut which is linked to this piece of data.
So whenever you access the variable, it will access this piece of data in an algebraic way.
So you can do them both separately or together, you could just do them by themselves where you declare
and do the assignment later, or what typically has happened is you do both at the same time.
So you would say the variable keyword denote the name of the variable.
Use the assignment operator and then.
Do the assignment.
So let's go look at our code.
OK, so let's show you an example of how we do this.
Variable operation.
We're going to start with declarations, so I'm just going to start with a let.
And say, let me count equals zero.
OK, so alternately, I could have started with just let count
and then assign count equals zero.
Let count, count equals zero.
OK, so this is a declaration
and this is an assignment.
OK, if you want to do them at the same time, let's do a separate one.
You could do a declaration and an assignment in the same stroke declaration and assignment.
OK, the declaration comes from declaring the variable by stating the variable keyword and then the
name of the variable.
The assignment comes from using the assignment operator and giving it a value.
So if I want, I can set that one to one.
Now we have the difference between the two of them and I can access my variables.
OK, so let's save that and see how this looks.
Zero and one.
OK, zoom in on our console so we can see that.
All right, so that is declaration and assignment in JavaScript, and you can do the same thing, some
other things you might need to understand is that you're going to run into some issues if you do this
with CONSED.
So if we say that just as is and give it a shot, there's a problem because cost is for constant variables
and that means you cannot edit them after they have been declared.
You need to initialize a const variable with a value.
So you need to give this some kind of a value to start out with, and it's going to keep that same value.
And you're not going to be able to edit that value because it's a concept.
And the purpose of const is precisely that is to have a variable where you set the value once and then
the code will not let you reset that value later, OK?
But as you can see now, we can.
Another thing you can note is that you cannot access count three before it's been initialized, so let's
move the console lock down.
Remember, you can hoist with far, but you cannot hoist with Consed, so one zero one and two.
There you go.
And we've got them.
OK, and of course, you can do the same thing with Hvar.
Now, the tricky the funny thing about far is that you could actually just declare a variable and reassign
it.
And this is by default, going to be a var, so it's the same as if you had the VA keyboard, so the
default behavior for variables in JavaScript is VA and that's why we have to specify if it's a letter
or a const.
OK, so we save that refresh count for is not defined.
So let's do that again.
OK, let's see that.
Let's get rid of that one.
All right, so you can see where that's being defined.
OK, so console log, count one, count two, count three and count four, in this case, this is a
var.
And so it'll behave the same exact way as we had before.
That's being specified as a bar and if you don't specify anything, it's still a bar.
OK, so that is our assignment for our lesson for declaration and assignment.
Now we go on to lesson six.