Coding for Kids

Variables

A variable is like a container in the computer's memory. You can put information into the container and take a look at whenever you like. You can change the information in a given container when you need to.

Coding for Kids Sidenote: Yes, it is possible to make a program that doesn't use any variables. You would have a program that isn't flexible at all and just repeats itself, like giving you a generic, "Hello there!" Mix in some random numbers and you could make an inefficient but effective program that gives you lotto numbers, but you can't control it for duplicate numbers. Take a look.
  • alert("Your lucky lotto numbers are...");
  • alert(Math.ceil(Math.random() * 60);
  • alert(Math.ceil(Math.random() * 60);
  • alert(Math.ceil(Math.random() * 60);
  • alert(Math.ceil(Math.random() * 60);
  • alert(Math.ceil(Math.random() * 60);
  • alert(Math.ceil(Math.random() * 60);
  • alert("Good luck!");

Variables are at the heart of all programming. If you're keeping track of a user's name, that's stored in a variable. A mage's hit points in an RPG, the gems you try to match in a match-three game like Candy Crush©, even a set of treasures or ammo you might pick up along the way; these are all stored in variable.

There are a few variable types in JavaScript. We will start with primitive type variables, which means they're not made up of smaller parts. They just are what they are. Other variables use these to create other useful constructs.

Declaring variables

To declare a variable, you must use the let keyword, followed by the name of the variable you're creating. Remember to use proper naming syntax and conventions. Previous versions of JavaScript used the var keyword, so you may see that in code. It works too, but let is now preferred.

Syntax:

Examples:

The const keyword

There is also the const keyword for declaring constants. If you have a value in your code that's never meant to be changed, then you should use this keyword.

Examples:

Notice the use of ALL_CAPS with the constants. This is not required, but it is considered the best practice for naming your constants. This helps them stand out as constants in your code. Using the const keyword with them makes the computer understand they're not supposed to change. If your code tries to change them later, it will throw an exception (error).

Why do we declare variables?

We always need to tell the computer what we're doing. Therefore, we need to declare our variables before we can use them.

To declare a variable, you start with the let keyword and then give the variable a name. As with all keywords, it has to be all lowercase.

You can end the statement there, telling the computer you want it to reserve memory for a variable you'll use later. This leaves the variable undefined. It's like saying you want to add more storage to your house but you're not sure what kind you need. See the page on null and undefined for more details.

Most of the time, however, you will assign a value to a variable when you declare it. It's a better practice because then the computer knows what you plan to put in it. You know, did you buy a vase, a set of drawers, or a backpack?

For some variable types, you need to tell the computer what kind it is before you can use it properly, such as with arrays and object.

The let keyword

There is a newer way to declare variables in JavaScript using let instead of var. It works the same way with one major difference. let forces you to keep the proper scope of your variables. If you're careful in what you're doing, you'll be fine either way.

The let keyword has been around for some time, but not all older browsers support it and you'd have to load a specific JavaScript version in order to use it. Most modern browsers understand it just fine. So it's up to you if you want to stick with the older var or the newer let. Ideally, you should stick with let.

What did the coding pirate yell to his crew? var mateys, let us eat!

—Dr. Wolf