This lesson, I'm going to be showing you how much fun you can have with variables and of course depends
on what your idea of fun is, what there are some different options for variables that you should be
aware of, such as setting, declaring variables with let and contest.
So this is new with ESX as well as different data types that are available within variables such as
booleans.
So what a boolean is, it's either true or false.
So it's just like the strings and the numbers that we've been working with in the earlier lessons when
we're declaring variables and assigning values to them.
And essentially it's like a switch.
So it can either be on or off, true or false.
So those are only two values that are available within a boolean.
And a lot of times booleans are used for conditions.
So they're ideal for conditions because you check to see if this condition is true.
And if it is, then you run a set of code.
So here we have an example of different data types.
So at first we come in and we declare the variable.
And we did mention that you don't have to assign a value, you can just declare a variable.
So when we're doing var message that's just declaring the variable in in order to be able to use it,
you need to declare it.
If you try to access it before you've declared it, then you're going to throw an error.
So at this point we can message and we can see the value of message and it's going to be undefined.
So this is the default state.
When you are setting a variable, the data type is undefined and undefined, is kind of the same thing
as null, but null is used.
And that's another data type that you can utilize when you are writing your code, and that can distinguish
and differentiate between undefined.
So this is a variable that's been set to hold no value and that's equal to null.
And typically we can set to undefined, but we're not going to be setting it to undefined because that's
going to lead to some confusion as the default is undefined.
So when we want to use a boolean.
So this is a great case for using a boolean.
So we have our declarative will we set it to our boolean value, which in this case is false, and then
we logout the boolean value.
So we're going to see false in the console log and then we update the mileage out to be true.
And then we check to see if Bilitis true.
And if it is, then it's going to output the message.
That's my light.
There's also you can declare multiple variables at the same time so we can do var and you can separate
them so you can have multiple variables declared at the same time.
And then these are all going to be equal to undefined, just as we do with the one variable.
And here's another example of the different data types that we've covered so far.
So we have a is equal to hello.
So notice the quotes around there.
So that indicates that it's a string B is equal to 10.
So there's no quotes around there that indicates a no and C is equal to false and true and false is
a boolean value.
So again, no quotes, otherwise it would be a string and you can output that value of a.
So there are different variable types and there's also different ways to declare variables.
So long with s six came Aulet and const.
So what Lenin and Konsta is they allow you to declare a variable and this is SCOP based.
So scope of JavaScript.
And of course we are going to cover this in more detail later on.
But for now it's important to understand that they're just different blocks of code that get run and
typically indicated by the curly brackets, as we see in the example over here.
So the difference between left and const is that left is a value that can change.
And const, as the name implies, is constant and is not meant to be changed.
And var was the initial way to declare a variable.
But now you're going to see that more and more coders are moving to let and const because of the control
over the block.
And essentially if you have a variable that you are changing, then you created a let.
And if you have a variable that you're not changing then you created as const and it gives you also
more control when you are utilizing your code to make sure that you're not unintentionally changing
a variable that shouldn't be changed.
And that's where const really shines.
And of course, more on scope later on in the course.
So now with let so really it is similar to VAR except that its scope based and that's a better way to
do things as you have more control over the variables.
So here we've got an example of that and really the same thing as when we're doing var where we are
assigning a value to it and then we're reassigning a new value to it.
Here we've got a code snippet that's an example of let where we see we're creating a message and this
is an example of scope and we are going to cover this in more detail later on.
But we do have a condition to check to see if a value is true.
And right now we've just defaulted it to true.
So it will automatically run this code.
And you can see that the message is going to be returning back and you're going to see that we've got
my message and then my message again.
But these two values in the console come out different.
So the first one that we're.
Pudding is going to be a new one and then we're going to see welcome when we've got the second console
message outputting the value of my message.
So we see that within this type of format, we're using it within the block scope and we are able to
declare that variable.
And then this variable is only equal to my message within this scope.
And that's why we get the output of new one and welcome in that order.
So contrary to let const, if we do the try to do the same thing as we did with let where we've got
the message, we're going to see that we're going to throw an error because const isn't going to allow
us to update that variable value.
And one thing to keep in mind is that you always need to declare variables before you try to use them
as an example of trying to use const.
And we see that it throws an error because it's not going to allow it to use const.
The challenge for this lesson is to declare a variable called message using let and then check to see
if it exists and if it doesn't exist.
If it exists, then we update its value.
And then lastly, we output that value into the console.
So go ahead and pause the video and try this out and I'll show you the solution.
Set up a variable called message and we can just leave a value of welcome inside and then console log
out the value of message.
You can see that we're outputting that value and then we're going to check to see if message exists.
And if it does, then we're going to reassign a new value for updated message and then we can console
log out message once again so we can see that value.
So let's try that.
And you can see we've got our initial message and then we've got the updated message.
And if we comment this out, we're going to see that we're trying to access message, but it doesn't
exist.
So we're going to throw that error.
So if you do see this reference error message is not defined, that means that we haven't declared message
and that's going to go through all of these.
If we try to define it, then we're not going to get a definition for it.
And you can also try to check to see if message is false or if message is true.
And you can see that initially message gets output, the boolean value, and then we're updating it
with the string value and also outputting it.
And one other thing to note as well, that the booleans and the numbers with chrome, they show up blue
and the string values, they show up black.
And you can also try that with const so you can update it with CONSED.
And you see that when you try to update the value of message that you're going to throw an error because
this variable is a constant variable and it's not able to be reassigned.
And even more bonus points if you tried just to declare the variable and try to see what gets produced
here.
And you can see that we get undefined because there's no value for message.
And even if we were to set message as equal to null, which was one of the data types, and we are going
to be covering that in more detail, you see that this message still doesn't translate to being true
because undefined a null as well as false, none of them are going to be translating into true.
So this actually has to have true or contain a value to be conditioned as true.
And then that's where you able to update those values.
Another thing, too, that you could have tried as well for even more bonus points.
So I know there's a lot of bonus points being handed out in this lesson.
So you can see that we've got our condition here.
So remember, this is a block of code.
So we're able to redefine, let here let message and this will give us a value of message.
So we just need to update this to be finally to be true.
And then that's going to actually run this block a code and you're going to see that this message is
referencing a different variable than this message, because over here we're able to update the value.
And then over here, when we're referencing it outside of the block code, we're still seeing that original
value that we had set.
So lots of bonus points in that example.
And there's more data types coming up in the next lesson.
So do try this out and get comfortable with that and more explanation coming up.