A bit more about Variables

Lets do a refresher. To declare a variable in JavaScript you can use 3 keywords:

You can also declare a variable, and not assign it anything at the beginning but later on assign a value to it.

            
            var lifeIsGood; // if you try to do something with this variable without any value assigned to it will tell you that it's undefined
            // later in the program
            lifeIsGood = true;
            
        

If you want to get fancy you can declare many variable in just one line. I always forget you can do this.

            
            var lifeIsGood = true, moneyIsPower = false, shame = 0; // with this you avoid having to repeat var 3 times
            
        

You can name a variable almost anything you want. There a couple of rules to keep in mind tho. A variable can have a number in the name, but it can't start with a number.

            
            var 8iscool = true; // not valid
            
        

That is an invalid name, so it will throw an error. As long as they start with a letter, or the underscore character, then you can throw in a number.

            
            var is_Cool8 = true; // valid
            
        

Another rule is that there can't be any special characters in it like ()*&^%$#@!,

The names you choose are case sensitive.

            
            var is_Cool8 = true;
            var IS_COOL8 = 'not the same';
            var is_cool8 = 'different';
            
        

The above three variable declarations are totally different. Case sensitivity is important to keep in mind because other languages are case insensitive. In PHP for example, a language used for server side web development, function names are case insensitive. Have no idea why this is, but be on the lookout for that.

The final rule is that you can't use reserved keyword. In other words, there are words or labels in the language that are already taken.

            
            let Infinity = infinity // not possible
            
        

You can however use the word infinity as a variable name because of case sensitivity.

You can take a look at the full list of reserved words in JavaScript here.

Var vs Let vs Const and what exactly is the problem with Var

This is the part where I'm still a bit fuzzy on, so bare with me. I've been reading Kyle Simpson's You Don't Know JS Yet book series and I kinda went back to understand what is supposedly wrong with var. According to Simpson, there is nothing wrong with var. The only 'issue' with var, is that it doesn't get blocked scoped, and that's an advanced topic if you're just starting out. The ES6 people have pushed to always use let, and to always avoid using var, but Simpson says that as long as you understand var's limitation, you should still be using it when appropriate.

So, to not get too much in the weeds with this, because I'm only beginning to understand this, var and let are pretty much equivalent when it comes to what they do, which is letting you store a value in a label you create.

            
            var label = 'one';
            let pencil = 2;
            
        

In that context they both behave the same. However, the const variable has a more clear distinction. What if I want to change the value 'one' on label variable later in the program. With var or let you can do this like so:

            
            label = 'two';
            pencil = 3';
            
        

You can do that with variables declared with var and let. (Note that we don't have to use the keywords var or let again to change the value because that isn't allowed.

            
            var label = 'one';
            let pencil = 2;

            let label = 'two';
            let pencil = 3';
            
        

The above is not allowed because it won't redeclare the variable with a new value. It will interpret this as trying to create a variable that already exists. Once a variable is created you can't duplicate it.

            
            const label = 'two';
            label = 'three'; // not allowed
            
        

The above won't work, because it's interpreting that expression as you trying to reassign the variable . Keep in mind the word reassign here. You can still change the value of a variable declared with const, you just can't reassign it.

Ok, so that's a bit confusing right? Let me see if I can break it down with some code lines:

            
            var thing = 'value';
            thing = 'newValue'; // reassignment is allowed with variables declared with var

            let thing = 'value';
            thing = 'newValue'; // reassignment is allowed with variables declared with let

            const thing = 'value';
            thing = 'newValue'; // not allowed with variables declared with const
            
        

So I think I'll leave it here. I recommend looking up the nuances between these three ways of declaring variables. The takeaway and the thing to keep in mind is that no matter which way you declare a variable, you can always change that value. The main difference with the const keyword is that you can't reassign the variable.