Welcome back. In this video tutorial, I am going to show you the difference between constants and variables in JavaScript. First of all, I am going to create a file called example, dot JavaScript. So I'm going to create example, dot j s, and I'm going to save this file on my desktop. Click Save, I'm going to use, I'm going to create a constant and to create a constant in JavaScript is you type const. And this is how we create a constant and then the name of the constants.
So for example, name constantly equals to john, john and then the semicolon. So this is a constant, and a variable in JavaScript can be var can be var name for example, name equals two. Susan, or it can also be a lead. But here I'm going to change this from name to name one so that we have no conflict. And this name to name two equals to Bob. So now we have three now we have three lines of code, the first one is a constant and the other two are variables.
So the difference between constants and variables in JavaScript is that constants cannot be changed later, but variables can be changed in runtime. So for example, now if I try to change the, if I try to change the constant name, I will not be able to So for example, if I change name to first of all, let's just let's just print all of these in the console. So console dot log, and then I'm going to print out all of these Name. And then I'm going to also print I'm going to do the same for, for name one and name two. gonna pick copy and paste and change this to one and two, two. And let's save and run.
And as you can see, after we are on we have john, Susan and Bob. But if I try to change the constant in the runtime, so for example, name equals instead of john, something like Matthew, Matthew with M capital, and if I save and run and if I save and run, as you can see, I get an error because I cannot change it later as you can see assignment to constant variable, you cannot change a constant once you have initialize it. So once you have a constant it cannot be changed. But if for example, I tried to change name one, two from, from Susan to Matthew, Matthew and try to change name to from Bob to to Alice and save and run. As you can see, john is first was first printed out and then Matthew and Alice the name one variable has been changed the the name to variable also has been changed.
So, keep in mind that if you are going to change a variable later you should specify it as a variable or a let do not make it a constant if you want to change it later. So constant always is always used for things that are not are never going to be changed later. Once we run our program