The Value in Learning Variables

by Nick Jensen on October 6, 2013

Join me as I move on to the third chapter of John Pollock’s JavaScript: A Beginner’s Guide. I must be honest, the chapter title “Using Variables” opens some old wounds. If I were to claim that math isn’t one of my strong points, it would be the understatement of the year! For myself, math is the antithesis of all my professional qualities. But enough about me. Let’s move on to Chapter Three’s essential lessons. Fortunately for us coders, JavaScript variables have less to do with number crunching and more to with assigning values to a code document. In other words, variables save programmers time by substituting any long line of code with a simple variable. For instance, let’s say you need to make a line of code, but you haven’t decided what value to assign it or the value constantly shifts over time. Variables allow you to substitute a value by putting a placeholder over the variable in question. You can then add a value to that variable such as in the following code:

var carprice;

The value of the car price might shift. Perhaps when it’s first released, the starting price is $20,000. But after a couple years, the price will decrease as the model gets older. To assign a value to the variable “carprice”, you simply add an equal sign (=) and the current price it’s being sold at. So, for that brand new car, you would write:

var carprice = 20000;

But what happens when you need to change the price later? If that variable was spread across multiple pages and you needed to update the price of the car in a hurry, you would be in a serious fix. Fortunately for you, the value of the car can easily be updated by changing the variable. Since the price of the car has fallen to $15,000, you need only change the value given to the variable:

var carprice = 15000;

By changing the value of the variable, you save yourself the time and energy of having to change the car price in each area it’s listed. That’s quite a time-saver, isn’t it? However, variable values encompass more than numbers. There are also string variables, Boolean variables, null variables, and undefined variables.

String variables are named so because they represent strings of text:

var firstgameconsole = “Super Nintendo Entertainment System”;

var myphone = “Samsung Galaxy S3”;

Boolean variables are simply variables which have true or false attributes:

var skyisblue = true;

var bloggingislame = false;

Null variables deal with empty objects (the book says I’ll learn more about objects in Chapter Nine).

var empty = null

Finally, undefined variables are variables without an initial value:

var mybrandnewcar

Notice that there isn’t a value attached to the variable. That’s because I haven’t assigned a value for it. It’s also because I don’t have a new car. I need to work on that.

So far, I have taken you just over halfway through Chapter 3. Join me next time when I finish Chapter 3 and perhaps even take a peek at Chapter 4. See you next time!

{ 0 comments… add one now }

Leave a Comment

Previous post:

Next post: