If you would like to receive weekly emails with articles like this one, email
Nora
The use of variables
are key to all programming languages. You may remember the x's and
y's from your high school algebra class. In programming, variables
have even more meaning than just some vague idea of an unknown value.
In programming, a variable is used as the name of a particular information
storage area. You can then use that variable name to call whatever
you happened to store there. If you throw away the old stuff and
put something new in the storage are, the variable calls the new
stuff.
Some programming languages have to know ahead
of time what you plan to store. They like to plan ahead of time
what size storage area you are renting. Telling these language what
you plan to store means that you have to declare a data
type. These types of programming languages are called strongly
typed languages. Other languages, such as JavaScript, could
care less what kind of stuff you are storing. They decide what size
space you need when you send it to the storage area. That is one
reason JavaScript is less complicated to learn than some programming languages.
However, there are still some things you will need to learn about
data types because there are some side effects to not telling what type of information you want to store.
In any case, the information still has a variable
name. Here is an exercise that 1) gathers information from a prompt
box, 2) puts it into a variable, 3) does a calculation, and 4) sends
the answer back in an alert.
Add this code to the <head> of a new web page
<script language="JavaScript">
var SquareThis = prompt("Enter a
number you would like to square.");
var Answer;
Answer = SquareThis * SquareThis;
alert(Answer);
</script>
Test
the script here.
Basic Programming Skills in this Bit
of Code
- When you do any kind of programming,
you have to think of what pieces of information the computer will
need to identify and hold on to. Then you make variables to hold
the information. In this example, we need to hold two pieces of
information, 1) the number that will be squared, 2) the answer.
- You tell JavaScript that you
want it to store information by naming var, a
space and a variable name.
- The first line of this script
does three jobs, 1) it tells JavaScript to set aside memory space
for the variable SquareThis, 2) it uses a pre-written function
to prompt the user for a number, 3) it takes whatever the user
types in the prompt box and puts it in the memory space for SquareThis.
- Notice that the function prompt
requires an argument just like the alert() did. Notice too that
the argument is in " ", which means that the argument
is a String data type.
- Now the script needs a variable
to hold the answer. This is done with "var Answer;"
- Everything is in place now to
do the math: "Answer = SquareThis * SquareThis;"
The part that tells the computer what kind of math to do is called
an operator. The main operators are +, -, *,
/ (add, subtract, multiply, divide). There are many other operators,
as well. A square is any number multiplied by itself; so this
formula multiplies SquareThis by SquareThis.
- The program has an answer, but
it needs to tell the user. Again, we will use an alert box.
-
Now change the formula to do something else.
Try adding another variable and another alert() to ask for another
number (or several.) Here are some operators for you to experiment
with:
Related Articles
- Using a Text Editor and a Browser to Create a Web Page
- What is JavaScript?
- Using a Pre-defined Function in JavaScript
- Variables, Math, Concatenation and Overloaded Operators
- Using a Prompt Box and Outputting to the Screen
- Basics about Forms
- Adding JavaScript to a Form