Variables are basically containers that you give a name to that hold any piece of information or data for you.
The two main parts of a variable are the name and its value. Because they are variables, their value is meant to vary over time, in other words change. Let's create our first one.
When you want to create a variable, you need to declare it. This tells the browser "hey, I want this name to represent a variable".
Let's say you want a variable to hold the number of apples you eat every day. It may look something like this:
You have now declared a variable called
apples, but have yet to give it a value.
Variable Naming Guidelines
Below are the rules for how to name your variables.
- The first character has to be a letter, an underscore, or a dollar sign.
- After the first character, you are now free to use numbers if you'd like.
var apples; var $apples; var _apples; var app_les; var apples4eva; var apple$;
var 1apples; var app les; var #apples; var apple%;
Also important to note is that variable names are case-sensitive, so
apples are different variables.
After you have declared a variable with a valid name, you can now initialize it, which is to give the variable its initial value.
The syntax for this is simple and intuitive. Let's say you look at a modest 384 memes a day. Here is how you would initialize your variable.
var memes = 384;
Likewise, if your variable is a string, like for example, the name of something, it would look like this:
var website = "Sabe";
How do you know if the variable initialization worked? Try running this in your console:
var memes = 384; var website = "Sabe"; console.log("I view " + memes + " memes a day."); console.log("I use " + website + " every day.");
You should get this as your output:
I view 384 memes a day. I use Sabe every day.
After a variable has been declared and initalized, you can always give it a new value by assigning it one. Because you already declared it, you don't need to declare it again.
To give the variables
website new values, it would look like this:
memes = 463; website = "Google";
Thus, when you run this:
var memes = 384; var website = "Sabe"; console.log("I view " + memes + " memes a day."); console.log("I use " + website + " every day."); memes = 463; // assigning a new value to memes website = "Google"; // assigning a new value to website console.log("I view " + memes + " memes a day."); console.log("I use " + website + " every day.");
Your output looks like this:
I view 384 memes a day. I use Sabe every day. I view 463 memes a day. I use Google every day.
var keyword, they are declared using
The rules for naming constants are the same for variables, however, it is common practice to capitalize all the letters. Here is an example of two constants:
const MEMES = 100; const WEBSITE = "Twitter"; console.log("I view " + MEMES + " memes a day."); console.log("I use " + WEBSITE + " every day.");
In this lesson, we used variables to hold strings and numbers, but in our next lesson, we'll learn what other cool things variables can hold!