Variables and Assignment0:00
Alright, let's start off with variables and assignment, which is a fundamental concept of any programming language. Think of a variable as a box that can hold some piece of information, which you can change if needed. Now, in JavaScript, there are three ways to declare a variable. Let me just open up DevTools here. We have var, let, and const. We'll talk about the differences in a second, but first let's start off with var. So if we want to declare a variable, we can say var and give it a name. So let's say firstName.
So if we want to declare a variable, we can say var and give it a name. So let's say firstName. So this will create a variable, or like I said, you can think of it as creating a box with nothing inside of it. Now if we want to give this variable a value, or if we want to put something inside of the box, then we can use the assignment operator, which is the equals symbol. Now don't get this confused with the mathematically equal sign, this is just saying that we want to assign a value to the variable, or we want to put something inside of the box. So we can say firstName =, and then give it some sort of value. Now we'll talk about all the different types of values in the next video, but for this
So we can say firstName equals, and then give it some sort of value. Now we'll talk about all the different types of values in the next video, but for this variable, I'll assign it to my name, which is a string, which is just a piece of text. We can also declare a variable and give it a value in one line. So if we say var, let's create a new variable named lastName, and we can assign it all in one line. So we can say equals, and I'll put my lastName here. And since we're using DevTools here, we can say lastName, and that will show the value of it. Now you can also change the value of the variable if you need to.
Using let and const1:26
of it. Now you can also change the value of the variable if you need to. For example, if I change my firstName, we can just say firstName. We can still use the assignment operator here, but just change the value. So let's change it to Bob. And firstName is now Bob. Okay, now let's talk about the other two types of variables. We have let and const. Let is very similar to var, but allows you to declare variables that are limited to the scope of a block.
Let is very similar to var, but allows you to declare variables that are limited to the scope of a block. Now since we're just starting out, we don't know what scopes and blocks are yet. So we'll talk about the differences once we have more knowledge under our belt. So for this video, let operates the same as var. So let firstName equals Andre, if you want to do it in one line. And you can also reassign it the same way. Now for const, const stands for constant, and you cannot reassign it.
And you can also reassign it the same way. Now for const, const stands for constant, and you cannot reassign it. You also have to make sure to initialize it with a value. So again, let me refresh here. So we can say const firstName and not give it a value. This should result in an error, and it does. It says missing initializer. So we have to give it a value. So const firstName = 'Andre'. And firstName is Andre, okay?
Variable Naming Rules2:39
So const firstName equals Andre. And firstName is Andre, okay? But like I said, const stands for constant, and we cannot reassign the value. So if I try to change my name here, it will not let us. We get this error of assignment to constant variable. Okay, now let's talk about the different characters you can use in your variable names. You can only use letters, so A to Z or uppercase as well. You can use all numbers, so 0 to 9. You can use underscores, or you can use the dollar sign.
You can use all numbers, so 0 to 9. You can use _, or you can use the $ sign. Anything else is not valid. Your variable names also cannot start with a number. So if I said let and try to name a variable with a number, say toName, that's not going to work. We get this error here. But it does work with letters, as you saw. And $ and _ does work. So if you wanted to, you can say $name, and that should work fine.
And $ and _ does work. So if you wanted to, you can say $name, and that should work fine. Or you can also say let _name. That should work fine as well. Generally speaking, you don't want to do that because there are two popular libraries that use the $ and _ within their syntax. So the $ is a symbol known for working with jQuery, and the _ is the symbol known for working with a library named lodash. So in general, just start your variable names with letters. Variable names are also case sensitive.
Case Sensitivity and Styles3:58
So in general, just start your variable names with letters. Variable names are also case sensitive. So if I said let name equals Bob, and if I said let name, all uppercase, equals Harry, these are two different variables. You can see we have name is Bob, lowercase, and name uppercase is Harry. So yeah, variable names are case sensitive. Let's also talk about the different types of case styles we can use for our variable names. So the most common is camelCase, where the first word starts with a lowercase letter, but any other word starts with an uppercase character.
So the most common is camelCase, where the first word starts with a lowercase letter, but any other word starts with an uppercase character. So we do this because space is not a valid character in our variable names. So to differentiate between words, we uppercase the first letter. So again, let me refresh here. Or you can just clear the console using this button, but I'll just refresh. So let's say let this is camelCase. So you can see the first word is lowercase, but the first character of every other word is uppercase. And this is a common convention within JavaScript.
every other word is uppercase. And this is a common convention within JavaScript. And generally, this is the approach I take for my variable names. There's also upperCamelCase, where the first word does have a capital for the first letter. But generally speaking, this is used for class names and constructors in JavaScript. There's also snake_case. So if you add an underscore within each word, this is snake_case. This will work too.
Choosing var/let/const5:38
You can still use it within strings, but for variable names, this is kebabCase because it looks like a kebab, but this won't work because there's dashes. So if I try this, it doesn't work. You'll often see kebabCase within HTML and CSS. So yeah, generally speaking, you want to use camelCase. So the first word is lowercase, or the first letter of the first word is lowercase, and then the first letter of each additional word is uppercase. Now, as to which type of variable you should use, var, let, or const, var has been around since JavaScript was created, and let and const were introduced in ES2015 or ES6, which is the modern version of JavaScript.
