Jonas: Let's now take a look at
the three different ways of declaring variables
in JavaScript.
So, up until this point, we have always used
the let keyword to declare variables.
However, there are also const and var.
Now, let and const were introduced in ES6,
and so they are modern JavaScript.
While the var keyword is the old way of declaring variables.
So, let's learn how they are different and which one to use
in which situations.
And let's, again, start by getting rid of this code.
Actually, all I will do is move this to the end
and in the future, I will just do this before recording
the next video.
So anyway, we use the let keyword to declare variables
that can change later so basically during the execution
of our program and that's exactly what we did here.
So, we used let to declare JavaScript is fun,
and then we changed that later from true to yes.
But for example, or another example I would say is
let's set the age to 30 at one point of the program.
And then in another point of the program,
we can change it to something else.
For example, when the person turns 31
and then the age of course changes from 30 to 31.
And so, it's perfectly okay to declare a variable
with let at one point in the program,
and then later assign a new value to it.
In technical terms, we call this reassigning a value to
a variable, or also we say that we mutate
the age variable in this case.
So, it was 30 and now it's 31,
and so we mutated the variable, okay?
So, that's a term that you will hear all the time
in the JavaScript world.
So, when we need to mutate a variable,
that's the perfect use case for using let
and that also counts for the case that we want
to declare empty variables.
So for example, as we did here, where we declared
an empty year and then later reassigned that variable
to 1991, and sometimes we actually do this in practice.
For example, when we want to declare all the variables
at the top of a file,
but only assign actual values to them later in the program,
for example, based on some condition.
Now, on the other hand, we use the const keyword
to declare variables that are not supposed to change
at any point in the future.
So, the value in a const variable cannot be changed.
For example, let's say the birth year, 1991 for example.
If we then tried to reassign that let's say to 1990,
it should not work.
So, let's test that, and indeed we get a type error.
So, assignment to constant variable.
And so, that's exactly what the const keyword does.
It creates a variable that we cannot reassign
or in technical terms, an immutable variable.
So, a variable that cannot be mutated, okay?
And the birth year is a perfect example for that
because in fact, the birth year of a person can never change
while the age of course can change.
And so that's why we used let for the age
and const for the birth year, okay?
That makes sense?
Now the fact that variables created
with const are immutable also means
that we cannot declare empty const variables.
For example, this is not legal.
So, let's comment out this one here, again with command
or control slash so that it doesn't get in our way here.
And then reload, and then we get
an error missing initializer in const declaration.
And so this error message means that
when using const, we need basically an initial value.
Alright, so just like here.
Now with these two different ways of declaring variables,
you will probably ask, should I use let,
or should I use const to declare a new variable?
Well, as a best practice for writing clean code,
I always recommend to use const by default and let only
when you are really sure that the variable needs to change
at some point in the future.
For example, when you have a variable
that's really never supposed to change like this birth year,
you should always use const.
Also, if you are sure
that the age variable is never changing inside your program,
you should declare it using const as well.
The reason for this is that it's a good practice
to have as little variable mutations
or variable changes as possible
because changing variables introduces a potential
to create bugs.
So, basically errors in your code.
So again, by default, always just use const and let,
when you're sure that the value of the variable needs
to change at some point in your code.
And so from now on, I will probably use the const way
of declaring variables as much as possible.
Now, there's also a third way in JavaScript
of declaring variables, which is the var keyword,
but this one should actually be completely avoided, okay?
However, we should still know how it works
for legacy reasons because you will see this maybe
in older code basis or some older tutorials
that you'll find online.
So, let's take a look at how it works.
So, var is basically the old way of defining variables,
prior to ES6.
And at first sight, it works actually pretty much
the same as let.
So, we can write var job equals programmer,
and then just like with let, we can change the value of
the variable later, just like this.
Let's take this one out and let's just reload,
and we didn't get any errors,
which means that it's basically allowed
to mutate this job variable when we use var, right?
Now, although it looks like var and let are very similar,
below the surface, they're actually pretty different.
And actually there are also many differences
between led const and var.
So, between all three of them,
and you will learn all about these differences later
in section seven,
because this is actually really important stuff.
And I know that I keep saying that you will learn
some topics later in the course,
but the truth is that learning is really not linear.
It cannot happen in a linear way.
So, you sometimes need more advanced things right now,
even without understanding them.
But that's not a problem
because you will understand them eventually.
So, right now it would really not be useful for you to know
that the let is block scoped
and that VAR is function-scoped, right?
Because you don't even know what a block
or what a function really is.
And so, we will learn about these differences
in section seven.
For now, what matters is that you should basically
never use var, okay?
Now just to finish, some people will probably point out
that we actually don't even have to declare variables at all
because it's actually not mandatory.
We can just write something like this,
last name equals Schmedtmann in my case.
And then we can just use this variable name in this case
by logging it to the console
and JavaScript will happily execute this script,
even without us declaring a variable, right?
We didn't use let nor const nor a var in this case,
but it still worked.
However, this is actually a pretty terrible idea
because this doesn't create a variable
in the current so-called scope.
Instead, JavaScript will create a property
on the global object.
And once again, you will understand a little bit later
what all of this means, but what matters is
that you should always properly declare variables, okay?
Never just write a variable like this
without really declaring it.
Great.
You're doing great progress here,
even though our code doesn't really do anything yet,
and we're not really writing any real applications yet.
Right, but we still need to get some fundamentals down
before we can do that.
And so that's really important, and that's why I'm stressing
all the little details about everything here.
And I hope you see how important that is
for your progress, okay?
And don't worry, we will get to the cool stuff even
a bit later in this first section, okay?
But for now in the next video,
let's talk about operators in JavaScript.