Variables are containers that you give a name to that hold any piece of information or data for you. Variables are used to store data in memory, and to access that data later.
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.
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.
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, however, you cannot start with a number.
let apples; let $apples; let _apples; let app_les; let apples4eva; let apple$;
let 1apples; let app les; let #apples; let 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. This is the value that it will have when you first declare it.
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.
let memes = 384;
Likewise, if your variable is a string, like for example, the name of something, it would look like this:
let website = "Sabe";
How do you know if the variable initialization worked? Try running this in your console:
const memes = 384; const 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 initialized, 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:
let memes = 384; let 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.
let 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.");
You might be wondering, what is the difference between
const? The difference between
const is that
const is a constant, meaning that it cannot be changed. This is different from
let which is a variable. Once you assign a value to a
const, you cannot change it, however, you can change the value of a
let variable. If you know you value will never change, you can use
const instead of
let. The use of