Explain How To Use Variables in Javascript

There is a reason why Flappy Bird counts how many pipes you can get through before you run in to one, or why Candy Crush counts how many moves you have left. That reason is variables! Practically all coding languages have their version of variables, but I am going to show you the Javascript version, because it’s one of the simplest coding languages in the world.

I should start out by telling you what a variable actually is. A variable is a value that may or may not change over the course of the program. Think of it as a placeholder for that value. It is useful when a part of the code is dependent on the variable’s value and you do not know what the value is going to be. It may seem confusing at first, but for now here is the basic syntax of a variable:

var (name of the variable) = (value);

That syntax is only used when declaring a variable for the first time. This is the syntax for when we want to change the value of  a variable:

(name of the variable) = (the new value)

Before I get to the next paragraph, let me just note that the name of the variable name is the opposite of a sentence — the first word is not capitalized but the rest of the words are. I do not no why this is supposed to happen, other than maybe to distinguish the words from each other since there is no space in between them.

I will use the Flappy Bird example again to clarify how a variable would work in the code of a program:

var pipesPassed = 0;

(when you pass through a pipe do) {

pipesPassed = pipesPassed + 1

}

Let’s analyze this code. This is assuming you are at the beginning of the game when you haven’t passed through a pipe yet, so the initial declaration of the variable states the value is equal to zero. The next line says that when you pass through a pipe, add one to pipesPassed’s current value to get its new value. The curly brackets tell the start and end of the code that is supposed to be done when the action (in this case passing through a pipe) is performed.

Variables are essential to every program. Without them there would be no way for anybody to see that you have the highest score in the arcade game you’re really good at. There are, of course, other ways to use variables than just the score of a video game. But those ways are a little bit more complicated. I might cover some of the more simpler ways in future blog posts. But for now just know that a variable’s main function is to store data that might be used later in the program.

Print Friendly, PDF & Email

2 thoughts on “Explain How To Use Variables in Javascript

  1. Great job explaining a complex topic! I really like the conversational tone here. It makes reading about this computer language easier for me than it would otherwise be 🙂

  2. I learned so much when reading this post! I am impressed with your knowledge in the area of coding! This is a great skill to know in our technology driven world.

Leave a Reply to Mrs. Bridges Cancel reply

Your email address will not be published. Required fields are marked *