Lesson # 3 Variables and Values |
# 3 Review Questions | # 3 Assignment |
Technically, variables are just places in the computer's memory where we can store information such as numbers and text for a future use. They work almost like a mailbox, where they could hold a variety of different pieces of information. A variable could hold a string of text such as:
memberName="Arnold Schwartzenegger";
Or a variable could hold a number value as in:
pi=3.14159;
The sample programs you have seen so far just display text, useful maybe, but only the beginning. We can use variables to perform useful calculations by storing numbers, and we can store text such as user login names, passwords, addresses, and much more. Variables themselves don't have a distinct data type, which is a fact that drives experienced programmers crazy. They get their type from the value that is stored in them, and values can be of several types, as shown in the chart below. The type is determined by the value that each variable is asked to store for us.
Value DataType | What it can store | Examples |
int
|
positive and negative whole numbers between -2^53 to +2^53 meaning from -9007199254740992 to +9007199254740992 | 0,1,2,3,-75, 32677, -30000, 25000000000 but not 3.14 or -25.5 |
float
|
floating point decimals in either decimal format or scientific notation between approximately + or - 1.7976931348623157 x 10^308 down to + or - 5 x 10 ^ -324 |
3.14159, -900234.2345, and in scientific notation (also known as exponential notation) 7.123 x 10^-22 , -2.07e17 (where 'e' means 10 raised to the power of 17) |
Boolean
|
These are nifty ways to hold true or false values, with false being considered a 0 and true is non-zero. Useful for testing for certain logical conditions. This type is named after George Boole, a famous mathematician. | true, false but not maybe. |
String
|
Any kind of text including letters, numbers, punctuation characters that probably won't be used for doing math. Examples are people's names, merchandise items for sale on e-bay, addresses, zip codes, social-security numbers, telephone numbers, the words to a song, etc.Strings need to be enclosed in single or double quotation marks. | "Good evening", "Arnold Schwartzenegger", "503-431-4000", "97223", "mypassw0rd" |
There are a few more types, but this will get us started.
We use the keyword var to create a variable. And we want to choose the names with care. Choose a name that lets you and others know the purpose and describe the variable's contents. Don't use single letter names such as x or y except in a few instances where it is common practice. Most programmers, for example, will use i for the name of a counting or index variable.
var lastName;
This is a statement that creates, or a variable named lastName and lets the browser know that we will be using this variable later to store, manipulate, or display some information. Use var only once per variable, when you declare it for the first time. After that just use its name. Here is another example:
var lastName = "Einstein";
This is a statement that not only creates a variable and assigns it a value of "Einstein" using the = sign. This is called initializing, and it is a very good practice. Once we assign it a value, we don't have any risk that some unknown or random value might be hanging out in our variables.
Another way to get a value stored in a variable is the prompt() method. It is a little like the alert() method in that it is part of the core JavaScript language, so we don't have to use it as part of an object, but it does something nice. It allows us to store a value in a variable for use later in the program. Here is an example of using the prompt() method. The statement looks like this:
var myVariable = prompt("Type in your favorite food.", "Anchovie Pizza");
There are some hard and fast rules for naming variables, and there are some common sense suggestions (which bad programmers will ignore).
First the rules you MUST obey!
Now the suggestions! These are commonly agreed practices that good programmers use - so use these suggestions to avoid looking like a dufus.
Check yourself here. Classify each of the variable names below as Valid or Not Valid, and think about why.
Showing Values on the Web Page
Before we can display our variables on the screen, we need to store some values in them. Remember that variables get their data type from the values we store in them.
|
So far we have only talked about using the "=" sign to get values stored in variables. That means that YOU, the programmer are the only one that can choose the values that we can use. Lets look at one way that JavaScript can let the user of the Web Page store values into our variable. A prompt() method can be used to allow the user to enter a value. Here is an example:
Here is the code and an explanation of how that works.
<input type="button"
name="exampleButton"
value="Example"
|
The input type of button is just a device to trigger the Javascript to run. We click the button, and the event onClick is triggered to execute. The name of the button is invisible to the user, but as programmers we will learn later how to use that name to identify components of a document.. The Value="Example" is the text the user sees on the button. We try to make it logical for the user. Here is the real action. Click is the event, and the event handler has the same name with 'on' attached. Hence - onClick. First a variable is declared named someData. It recieves its value from the prompt function that tells the user to enter some data, then sends the value of that data to the variable. Last, an alert function shows that data back to the user. |
Here is a sample of a small HTML form, with a place to get some input, and then do a little math, then show the output. Try it for yourself and then we will break it down and explain the details.