Declaring Variables and Constants

Declaring Variables and Constants

Pretty much every single piece of software requires the use of variables. Variables are a fundamental part of any programming language, and JavaScript is no different.

Variables

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!

Declaration

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:

	
    var apples;
	

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.

  1. The first character has to be a letter, an underscore, or a dollar sign.
  2. After the first character, you are now free to use numbers if you'd like.

Examples of valid JavaScript variable names:

	
    var apples;
    var $apples;
    var _apples;
    var app_les;
    var apples4eva;
    var apple$;
	

And examples of invalid JavaScript variable names:

	
    var 1apples;
    var app les;
    var #apples;
    var apple%;
	

Also important to note is that variable names are case-sensitive, so Apples and apples are different variables.

Initialization

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 needed a string, like for example, a 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 have seen this as the output:

	
    I view 384 memes a day.
    I use Sabe every day.
	

Assignment

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 memes and 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;
    website = "Google";

    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.
	

Constants

Constants in JavaScript are basically variables except that their values are meant to be unchanging after being initialized. Instead of using the var keyword, they are declared using const.

The rules for nameing 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.");
	
The output using constants.
The output using constants.

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!