So we have finished considering our helpers, and now it's time to actually start describing updated
and new features of JavaScript, which are represented in its version.
Generally, I would like to note that there was almost nothing in ECMAScript 2015 or ESX version that
could not be done using S5.
So because of that, throughout this section, in most cases we will compare year five and six code
to each other.
The first thing that we're going to discuss is the most popular change in ethics, which is variable
declaration, you already know how to declare variable.
We have done it dozens of times.
For that, we need to use keyword war.
With name and value.
Let's.
Look them in council.
So here we have John and 28.
In year five, we are able to change dynamically values of the variables, for example, if we assign
to name and age variables the other values like Nick and.
29 to age.
Then they will be dynamically changed.
This stuff is different in year six and let's see how it actually works in new version of JavaScript.
There are two types of variable declarations left and cost where we want to update the content of variable
dynamically.
We should use keyword LAT, but if we want to never change the content of the variable, then we have
to use const, which actually stands for constant.
In this case, I'm going to declare any variable with const.
Because you agree that in most cases the name of the person never changes.
As for the age, it's definitely changed.
Therefore, I'm going to declare it with LET.
Actually, this is the main and the only difference between that and declarations.
All right.
Let's reload the page and you see that we have an error and the problem is an assignment to constant
variable.
So as we said, when you assign the value to constants variable, then you can never change it.
It doesn't matter if you write your neck or its initial value, John.
Anyway, we will get an error.
So you cannot use assignment operator.
I mean, equals sign with the same constant variable twice.
OK, as for the constant variable, I would like to note that recently there was a special convention
in JavaScript.
If developers wanted to never change the values of variables for that, they were using capital letters
with variable names like this.
All right.
Let's delete this line of code.
Reload and we have John and 29, so in case of that variable, we do not have any problem with updating
the value of the variable.
At a glance, it seems that that declaration works exactly as VAR Declaration does.
Actually, I can tell you that they work in a similar way, but there are some differences between those
declarations.
All right, let's remove this code.
So as we know, in year five, we can create a variable, for example, person.
And assigned to its value, John Smith.
And then using the same verbal name, we can overwrite new value to old one.
So if we check in councilperson variable.
Of course, we will get to.
Let's see what happens in Essex, change of our keyboards into let.
That revolt and you see that we have an error, which tells us that person has already been declared
so which means that in Essex you are not able to overwrite value of the same variables.
And actually, it's really helpful and great picture because in practice, when you deal with large
code, it will allow us to avoid naming conflicts with variables.
All right, let's talk about another difference between that and Hvar, that's the latest line of code
and change that into VAR.
And running council.
Rite Aid before its declaration.
So you see that we have undefined because of hoisting, and I'm sure that you remember what hoisting
think is if we change over into that.
And as a result, we have an error saying that person is not defined, so JavaScript engine, instead
of undefined directly gave us an error.
And in my opinion, it's better.
Actually, JavaScript engine knows that there exists this variable, but it doesn't allow us to richet.
This is known as temporal, that zone.
It's not actually official term, but most developers use it.
We will get absolutely the same results if we use constant instead of that.
All right, that's the latest Cote's I want to show you one more difference between left and cost.
Many developers actually do not mention it, but I want to refer to it as we remember, when we create
variable on global level, it actually is stored as a property of global window object.
So if we write for a equals the 10.
And then check the window object.
We will find variable as it's property.
Also, if we write we know dot a, then we will get time.
Let's see what happens in case of that and constant change.
Varin into that.
That reload and check again window object.
You see that we cannot find here a also if we write window dot a.
That we will get undefiled.
The exact same happens if we use constant instead of let.
I won't be sold as a property of global we don't object and also.
Window dot a.
Will give us defined.
So in its sixth version, let and cost variables declared on global level are not stored as properties
of global window object, but regardless of that, they act as global variables if they are declared
on global level like it's in case of vah.
All right.
Let's go ahead and talk about the main difference between Vialet and cost.
As we know in is five variables are functions skulked.
Let's recall what it means.
Creates Function A and inside Kurla prices.
A variable be.
With the value, hello.
And then outside of function, run in council, the barrier will be.
If we reload, we will get an error, which tells us speed is not defined.
So in this case, verbal B is a function scope and we cannot access on it from outside function.
The same happens with LET.
And also with constant.
In both cases, we have is not defined.
So let's add cost variables are functions coped as well, but at the same time, they're blocks coalbed
variable declarations and it's the main difference between Vah and Letton constant.
Let's see what this exactly means.
Delete this function and create if statement.
And as a condition, let's just insert here through.
That inside culebras is a variable for steam.
And assigned to it to value John.
Then run it in council from outside if statement.
Revolt, and, of course, it works.
We have John here.
If we change over into that.
That remote.
You see that we have an error saying that first name is not defined.
The same will happen if we use Carlstadt instead of that.
So, as we said, let in constant variables are blocks kept in block, I mean, everything that is surrounded
by color braces.
So if we create outside of if statements variable first name using that declaration.
That we will have Nic in council, regardless of that, those variables have the same names, first
name, they are completely different variables.
If we run inside block first name in Castle.
Then we will get John and Nick, so it means that those are different variables.
All right.
The next thing that I want to show you is that in case of that declaration, we are able to declare
it outside of block and then assign the value inside block.
So if we just get rid of inside, block the let keyboard and also delete this constant dialogue, then
we will get the updated value of variable first name as John.
But in case of constant.
We cannot do that because we cannot we assign to concepts variable, new value.
But in case of consent, there was one exception when we deal with objects, let's change back constantly
to that and create new objects with constant declaration, right person.
And in 30 years, some properties name as Jane and then the age as 18.
Then if statements change the value of age property and make it 19.
And then running castle person object.
You see that the value of property is changed from 18 to 19, so when we use the declaration with objects,
we can manipulate all the values of the properties of that object.
Makes sense.
All right.
Now I'm going to demonstrate what happens when we deal with a loop's, so create simple for loop, right?
Verbal eye equals zero, then as a condition I'm going to write your eye is less than five.
And then the increment I plus plus.
Inside Collaborationists, that's Log-in Castle.
I.
Let's reload and we have numbers from zero to four in case of our declaration, we are able to access
variable from outside of volume.
So if we write, cancel that log, I.
Then we will get five.
If you expected four or something else instead of five, I would like to remind you that after a situation
I is increased to five, condition became false.
So Loopt has stopped execution and that's why the value of variable AI has been maintained as five.
All right, let's change of our intellect.
Revolt.
And you see that at first we have numbers from zero to four, that's because of first class Deathlok,
which is placed inside block, but then we have an error.
And the reason is that from outside of Falbe, we cannot access unlet because it's blocks code.
And for a look like a false statement and functions also creates block.
All right.
When we use that declaration on each iteration, new variable is created.
And in order to prove that, I'm going to change slightly this example.
In JavaScript, we have built in a method called Septime Out, which actually works in almost a similar
way as set integral method.
The difference is that it executes function once after delite set time out method texte parameters.
First one is callback function and the second one is amount of time, which actually defines delay before
function is executed and it's expressed in milliseconds.
So.
Right.
Set time out then insert anonymous function.
Then place this castle, that log in the braces.
In order to see better how Falbe works and how each iteration is done as a second parameter, I'm going
to place here, I multiplied.
Two, 500 milliseconds and also change led into VAR.
Delete this cancel log.
Reload.
And he said that we have No five, five times that happens because one function is invoked at this moment
for a loop is already finished.
The value of verbal is five.
And because of the delay, we get it five times.
Let's change over into that and see what happens.
You see that numbers are apart from zero to four.
It's kind of strange, right?
So let me explain what has happened here in case of that variable.
We know that it's blocks called.
So on each iteration, new variable is created.
So on each function execution, there are different variables.
And what you see here, I mean, numbers from zero to four, those are values of five different variables.
Makes sense.
All right, you may wonder what happens if we use customs instead of let some of you may think that
because of that, after each iteration new variable is created, logically, we should get the same
results.
Let's see what happens.
Change that into cost.
And you see that we have an error and also zero.
So this error is cost because we cannot modify the value of constant.
Initially, its value is zero.
On first iteration, it gives us zero.
That's why we see in Council zero.
But at the end of the first iteration, when the JavaScript engine sees a plus plus, you know, that's
a plus plus is the same as I equals to I plus one.
So which means that assignment operator comes into play and because of that we are not able to assign
new value to cost variable.
That's the reason why we have an error here.
Make sense.
All right, before we finish this lecture, I'm going to show you one more thing.
So as you remember, in year five, we could create blocks for data privacy using immediately invoked
function expressions.
We have seen it clearly in our quiz application.
In order to create APHIS, we have to open parentheses then right function.
Then inside collaborationist, let's create variable A, which equals to 10.
Then invoked the function and outside of function, right, consider log a.
Of course, we have an error because we cannot get access on variable aid from outside Effi, but in
Essex this stuff became much more simpler.
We don't need to create, if it's anymore for creating blocks.
We have just to use Calabrese.
And let's create here, let's be which equates to, for example, 30, if we run it in council from
outside of block.
Then we will get an error because we cannot access a variable, be from outside of block, obviously
the same happens with constant declaration.
You see that again, we have an error.
But there is one thing to note, if we use your.
Vaki, but than those curly braces won't work because, again, verticalization is not Bloks called.
All right, after all of this stuff, you may have a question when and where to use Örvar Let or concerned
some of the developers are coming to use a in cost and ignore the declaration if you write ethics code.
But some of them recommend the use of our declaration only in case of declaring global variables.
I suggest to use Latin counts and ignore our declaration when you writing ethics, but it's up to you,
you can take into account both recommendations also generally use constant declarations in most cases.
But when you know that you would have to update dynamically values of variables that always declare
them using that.
All right, that's it.
See you in the next video.