Some Background on JavaScript Variables
Introduction.
Since you have seen variables and basic operations on variables in a
variety of languages by the time you take this course I will not spend
very much time on them. As is often the case when working in web
programming variable definitions are actually much simpler than they are
within more client oriented application development languages such as
C++, Java or Visual Basic. Though the variant variable type in
Visual Basic is very much like the overall inclusive type declaration we
will use in JavaScript.
For code readability and portability you should always declare your variables using the var keyword whenever you first use a variable. The Naming Conventions. You should always give your variables names that reflect what they hold. For example, calling something unknown_interest_rate or UnknownInterestRate is much more helpful than i. There are three basic rules for naming variables.
Arithmetic With Variables. The arithmetic with variables is pretty standard and I encourage the use of parentheses when you need to ensure the calculation precedents. The basic operators are contained in the following table. |
Operation | Symbol |
addition | + |
subtraction | - |
multiplication | * |
division | / |
modulo | % |
negation | - |
increment | ++ |
decrement | -- |
Now I will present an example that you should save as SecondsPerDay.html. This script will also server to illustrate some other basic programming approaches in JavaScript. |
<html> <head> <title>Seconds in a Day</title> <script language="JavaScript">
</script> <font size = 4>
</script> |
This code does very little other than set variables for
the number of seconds per minute, the number of minutes per hour, and
the number of hours per day and then use them to calculate the number of
seconds per day. It also shows you how to display the results
using the write( ) function.
After uploading this page and going to it on your web with a web browser, you should see something like the following. Notice that I have done a little bit of formatting of the output. |