Variables are used to store data values. These values can later be accessed by just using the variable name they were stored in. An example of a variable is as follows:
var age = 38; console.log(age);//logs 38
In the code above, var is the way to declare a variable, age is the name of the variable, and the variable is given the value of 38 by using the equals sign. This equals sign is known as an assignment operator. The value 38 is a number, which is a data type that is different than the string introduced in Part 1. Numbers are not put inside quotation marks. Data types will be discussed further in another section.
- var is the older way to declare a variable, but using var is still valid. Some older code that you may work with in the future may use this convention, so it is important to know. Values assigned to variables declared with var can be reassigned or changed. When possible, it is best to use the next two variable types, const and let.
- const is used to declare a variable that will not be changed. An example is as follows:
const firstName = "David";
Since my first name will never change, const would be appropriate to use. You may also notice that I named the variable so that it is self-describing. This is a good coding convention because it helps other Developers easily identify what the variable is being used for.
- let is used to declare a variable that can be changed. An example is as follows:
let age = 38;
Since age is something that will definitely change, using let would be appropriate. Remember that let is the variable declaration, age is the variable name that you will use to refer to the variable later in the code, the equals sign is the assignment operator, and the number 38 is the value. Also, this statement is closed by a semi-colon.
You can now use replit to practice what you have learned! Here's how:
- In the white area type:
const name = "Put your name here";
- Below that type:
let age = a number goes here;
Since your name will not change we used const, and since your age will change we used let.
- Below that, type console.log(name);
- Below that, type console.log(age);
- Click run at the top.
As you can see above, we used just the variable names without quotation marks to log the values saved in the variables to the console. We could have also used var for both of these variables, but using let and const is a better practice.
Also, you could have combined the variables into one console.log(). This wouldn't have made much sense to display them that way, but it is good to be familiar with the fact that you can log multiple variables to the console in one line by separating them with a comma. This is demonstrated below:
As stated previously, you can reassign values to variables you have declared with let or var. This is demonstrated below:
Please take some time to play around in replit and practice assigning variables and logging them to the console. Make sure you are using good coding conventions. Take note of the white space placed in each statement. Also, use blank lines to separate one block of logically related code from another. Further reading on styling conventions can be found here.