A beginner’s guide to declaring variables
As you can see, we follow just that format. Our declaration is var and we have named our variable a. We always include the equals, as this is what our variable a is equaling. And last we have our value which has been set to 10. We have just declared our first variable! What were to happen if we wanted to increase a’s value by 10 using an if statement? Well, we have four options available to us so let’s take a look at all of them.
Okay, so I may have stretched two into four but with good reason as you will see later. In the first two examples we are simply reassigning a to a plus 10 and in the second two we are reassigning a to be 20 outright. As you might expect, in all instances, if we were to console log outside of the if statement a should still equal 20. Right?
As you can see, we have reassigned the value of b to 20 but have achieved it two different ways. Haven’t I left out an example? Well yes, but to help you better understand the difference. Let’s take a look at it now.
What do you think it will return?
We got an error, but what is that error telling us? Well clearly it is telling us that we cannot access our variable of b before it is initialized. Our error picture also gives us a visual of where our code is stopping. After we have declared our variable of b it should equal b, and then stops. This is because b is being initialized right then and there. We are not actually calling on our initial value of b, we are making an entirely new variable of b within our if statement. For a visual let’s see what b would be outside of the if statement.
As you can see our variable of b is still equal to its initial value of 10. This is because let is a block scoped variable. When we declared b equal to the value of 20 within our if statement, we created a new variable named b within that block. Once we exit the if statement, we exit that block and return to the outer most block where our variable of b is equal to 10. But what would happen if we just reassigned the values like in the other two examples?
In both cases, because we are not initializing a new instance of b within our if statement, i.e. creating a new block so to speak. We are simply just reassigning it’s value on the block that it is first being initialized on. But we’ve still got one more member of the family to cover so hold on.
Up last but certainly not to be overlooked is const, which as you could probably guess stands for constant variable. It sounds kind of scary, what does that mean exactly? Well to put it very simply and a matter of fact, a constant variable cannot be changed once it has been declared. So, in a sense it could be scary, should you need to change that variables value. Let’s take a look at an example below.
Wait I was able to do this though? Well that is because similar to let, const is a block scoped variable. Meaning that we have in actuality just created a new constant of c equal to 20, but only within our if statement. Should we try to make the value c plus 10 of our const variable of c, we will be hit with the same error of not being able to access our variable of c before it is initialized. But what if we tried to try to reassign it?
I would like to thank you for reading along and hope that you have learned something new! Keep an eye out for more articles the future!
If you would like to view a copy of these examples to play around with for yourself or just for reference, you can find the link for the GitHub Repo here.