2.1 Variables

Welcome to the JavaScript course where we will see the variables that are one of the most fundamental concepts of Javascript and any other programming language. In programming, we use a variable to temporarily store data in the memory of a computer. We store our data somewhere and give a name to this memory location, with this name we can read the data at the given location in the future. Let's take an example, think about the boxes you use to store your stuff. You put your stuff in various boxes and you put a label on each box, with this you can easily find your stuff. A variable is like a box, what we put inside the box is the value we assign to a variable, its data and the label we put on the box is the name of our variable. Now let's see this in the code. Here in index.js I'm going to declare a variable, previously before ES6 version we used the keyword "var" to declare a variable but with "var" we have problems that I'll explain later in a future video. From version ES6 the best practice is to use the keyword "let" to declare a variable. Now we have to give this variable a name or an identifier, like the label we put on a box, I'll call it "name". And don't forget to end the declaration with a ";" Console.log(name); We save and we'll see what happens. We get an "undefined". We can see that we have not defined the value of our "name". We will therefore initialize it. So in javascript the most common to use simple quotes for our strings. 'Brahim' In this example, we have declared a variable called "name" and we have set it to this value. There are a few rules for naming these variables, the first is that it can't be a reserved keyword. In JavaScript we have reserved keywords like let, if, else, var and so on. For example we can't modify this variable by one of them, for example if we replace "name" by "if", we can see that we have a syntax error The second rule is that they must have a meaning, they must have a significant name like significant labels, we avoid writing for example. let a or let b etc... Use descriptive and meaningful names for the type of data you are storing in this memory location. The third rule is that variables cannot start with a number, for example you cannot put let 2name. The fourth rule is that variables can't contain hyphens, so if you have several words, you have to put them together, for example you can call a variable "myName". This is aNotation that we call CamelCase. CamelCase is a convention we use in JavaScript to name our variables, so the first letter must start with a lowercase and the first letter of each subsequent word must start with an uppercase. Another thing you need to know about these variable names is that they are case sensitive, so if for example I declare a new variable with this time an uppercase first letter. it will be different . from our first variable that we initialized earlier. Finally, the last thing you need to know about these variables is that there are two ways to declare several variables, you can declare them on one line by separating them with a "," 'Toto' myFirstName. Alex'. You can also declare each variable on a line. So this is the most modern and common practice. That's it for this video on variables, let's meet again for a next video we will talk about constants.