How to Use Variables in CodeHS

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?

According to CodeHS.gitbooks.io, in JavaScript, variables are like small boxes where you are able to store values that you need later. There is a name and a value in every variable.

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

53

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:

function start(){

// Declare and Assign the variable

var numNails = 53;

// Print out the number of nails

println(numNails);

// Reassign the number of nails

numNails = 75;

println(numNails);

}

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.

  1. The variable numNails is declared and assigned the value 53.
  2. The number of nails is printed out to the output box.
  3. numNails is assigned the value 75.
  4. 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.

var numNails;

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.

  • Numbers
    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.

  • Booleans
    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.

  • Strings
    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!” ;

Well, that’s the information about Variables in CodeHS including using it according to the CodeHS.gitbooks.io. If you want to know more, you are able to access it by yourself. You are also able to watch a video on Youtube entitled Variables on CodeHS Youtube channel. This video was uploaded on June 26th, 2019 and it has been watched more than 9k times. The information about variables can also be found in the CodeHS site in the tutorial page where the title is Variables in JavaScript.

Leave a Reply

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