Do you have a dream to be able to make a website or anything related to it? Well, in this modern era, it seems that everything is not far from technology. So, the use of apps or websites is done in almost everything.
CodeHS is present to fulfill your dream to have the ability of coding. CodeHS, as explained on Wikipedia, is an interactive online learning platform and it offers computer science and programming instructions for schools and individuals.
In CodeHS, one of the materials that you will learn is Variables. What is it and how to use it?
What Are Variables?
Let’s take an example. Let’s say that you are going to the hardware store to buy nails since you are building a bookshelf. But, because you are going to the store at the end of the day, you make a decision to write down the amount of nails that you need from the store. So, in your piece of paper, it may be written like this:
Number of Nails I Need
Analogically, the piece of paper is like a variable and the paper is like a box. The box has a name and the name is “Number of Nails I Need”. It stores a value where the value is “53”.
How to Use Variables
According to the CodeHS Gitbooks, here is the explanation about using variables.
If all we are able to do is declare variables and initialize them, variables are only useful a little bit. Let’s say that you want to change the value of the variable. You decide that you want to get more nails because you bend some. Then, you want to make a change to the value of numNails from 53 to 75.
You are able to do that by assigning the variable a new value.
numNails = 75;
Now, in your box, there is a 75 in it.
If you want to check the value that is currently in the variable, you just use its name. For example, if you want to print out the number of nails you need, your program would be like this:
// Declare and Assign the variable
var numNails = 53;
// Print out the number of nails
// Reassign the number of nails
numNails = 75;
In the code above, the lines which are started with “//” are comments and are ignored when the code is run. They are just there for a human looking at the code. Now, you have to walk through this code line by line to understand more.
- The variable numNails is declared and assigned the value 53.
- The number of nails is printed out to the output box.
- numNails is assigned the value 75.
- The value of numNails is printed out again.
So, the output of this program could be like this:
53 </br> 75
The Difference Between Declaring and Initializing
A variable is declared by using the keyword var. Let’s say that you make a variable for the number of nails that you need.
Now, you have an empty storage place where can store the number of nails.
It is the same as having an empty sheet of paper. However, this variable does not have a value yet. It is called “uninitialized”.
Let’s say that you want to declare the variable and initialize it. If so, you are able to write this:
var numNails = 53;
Now, you have a variable named numNails with the value 53. Essentially, you have put a 53 in the box named numNails.
So, the meaning of declaring a variable is creating and naming an empty box to put things in later and the meaning of initializing a variable is creating, naming and filling a box.
Types of Variables
As explained above, you are able to declare variables, initialize variables and assign values to variables. But, do you know what kind of values that you are able to assign to variables? Variables are able to hold numbers, booleans or strings. The type of variable is determined by the type of value that its box contains.
A number is the simplest variable. Number variables have the ability to hold Integers or Floats. Integers are positive or negative whole numbers. Integers are numbers such as -3, -1, 0, 1, or 114. How about Floats? They are numbers which have a decimal point and they are able to be positive or negative numbers. Floats are numbers such as -2.5, 3.14, or 0.25. Integers and floats are declared in the same way.
var numNails = 10;
var costOfNails = 2.50;
var divisor = 3.0;
The difference between Integers and Floats is important when you are doing math with them. Let’s say that you want the result of your calculation to be a decimal. If so, one of your numbers must be a float. It can be done by initializing or assigning a variable like the variable divisor.
What are booleans? They are true or false values.
var loggedIn = true;
var gameOver = false;
The words “true” and “false” as you can see above are keywords.
What are strings? They are text or words. The entire value of a string is put in the box of variable. All of the text that you want to go into your string needs to be between quotes.
var name = “Alice” ;
var question = “Do you like to program?” ;
var sentence = “These 3 variables are strings!” ;