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 than some programming languages.
However, there are still some things you will need to learn about
data types.
1. Type this inside the <body> on a web page.
<script language="JavaScript">
var FirstNumber = Put a number here; * Choose your own number
var SecondNumber = Put a number here; * Choose your own number
var Answer;
Answer = FirstNumber + SecondNumber;
alert(Answer);
</script>
2. View the web page in a browser.
You should see an Alert box with the answer.
3. Put words in quotation marks instead of the numbers. Ex. var FirstNumber = "These are words.";
4. View the page in a browser - did JavaScript add them?
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 three pieces of
information: the two numbers that will be added and the answer.
- You tell JavaScript that you
want it to store information by naming var, a
space and a variable name.
- The + operator only adds if it is between two numbers. When it is between two words, or a word and a number, it concatenates them. This is handy for joining first and last names pulled from a database.
- Since the + operator does two different functions, depending on the context, it is called an overloaded operator. This is actually a weakness in JavaScript, which results in unintended consequences.
|
Operator
|
Function
|
|
+
|
Adds two numbers, or concatenates them, if one is not a number. |
|
-
|
Subtracts two numbers |
|
/
|
Divides two numbers |
|
++
|
Adds one to a number
(only needs one variable) |
|
--
|
Subracts one from a number
(only needs one variable) |
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