Beginning Javascript Course
Grammer and Punctuation
Just like any other programming language, JavaScript is capable of performing a vast amount of functions - not just being read as a text, or even a hypertext page. The ability to perform functions, requires that it has commands and protocols that can be looped, counted, allow for input and output functions, etc. The Commands in JavaScript, like in any other programming language, are the Key Words that generate a response from the computer. Much like in BASIC - the GOTO command, FOR-NEXT loops and so forth. In Basic you would have statements like:
FOR X = 1 to 10;
Print X;
Next X
Statements are like sentences in programming. They are made up of words (like For, Print, Next), and other functions and expressions ( X = 1 to 10 ). All put together, it forms a complete programming sentence.
In JavaScript, the same exists. The sentences are called "statements". Just like in English, sentences must end in a punctuation mark. I JavaScript, the end punctuation marks are not periods, but semicolons. If you try to end a statement in Javascript without a semicolon, you will most likely get an error message. In general, line-breaks and spaces are ignored by JavaScript.
Variables
Having the ability to do mathematical functions makes JavaScript one step closer to a real programming language than pure HTML. Mathematical functions are fairly useless, though, if you can not interact with the computer. Having only fixed integers limits the way you can work. Variables make it possible to input and work with changable values. Variables allow you to store information temporarily, that can be changed later. Before using a variable, it must be declared. This is done (usually) in the header part of the web page.
There are written like this, usually in the header part of the web page:
var variableName [=value] [..., variableName [=value]]
Example:
var X = 12;
var FutureVar2 = "";
var New_Monster_Level = 8;
var ProgName = "HelloWorld"
Variables must start with a letter, but can numbers after that. Always assume that variables are CaSe_SensiTive, because in some browsers, your JavaScript may not work correctly if you change cases. Also - always try to make your variable names descriptive - so that you have a clue what they mean 2 years from now when you are debugging an old piece of code.
Ok, now that we have the variables concept down, how can we use them in a program?
I thought you'd never ask! Let's assume we want our program to calculate how many hours there are in a year. Just for argument's sake, we'll assume it's leap year. Here's our code:
<head>
<title>
Title of your Web Page
</title>
<script language="JavaScript">
var DaysLeapYear = 365;
var HoursPrDay = 24;
var HoursPrYr = HoursPrDay * DaysLeapYear;
</script>
</head>
The first thing this program would do is declare a variable "DaysLeapYear", and set its value to 365. Next it would declare another variable "HoursPrDay" and set its value to 24. Finally, it would multiply those two variables and give the result the variable name of HoursPrYr.
So we see that unlike HTML, JavaScript has the ability to perform mathematical functions like multiplication.
Note that the "*" symbol (asterisk) represents multiplication. This is called the multiplication operator. There are also operators for other mathematical functions, as follows:
| addition/subtraction | +
| - | |
| |
| multiply/divide | *
| / | %
| | |
| negation/increment | ++
| - | !
| ~ | -
|
| shift | <<
| >> | >>>
| | |
| relational | <
| > | <=
| >= | |
| equality | ==
| != |
| |
|
| bitwise AND | &
| |
| |
|
| bitwise XOR | ^
| |
| |
|
| bitwise OR | |
| |
| |
|
| logical AND | &&
| |
| |
|
| logical OR | ||
| |
| |
|
| conditional | ?:
| |
| |
|
| assignment | =
| op= |
| |
|
| comma | , |
|
| |
|
| Call, member | .
| [] | ()
| | |
This is only a basic level course, so, although we do not discuss all of these operators in detail, it is good to know that they exist.
Once variables are declared in the header of our document, we can begin using them. If we need to declare a variable that we will use later in the web page, but don't want to assign a value to it now, we can simply give it an "empty set" (eg. var variable = "" ). We would want to do this in the case that the variable might represent a value for something which hasn't occurred yet. You see, if a browser runs into a variable that was not delcared, you might run into "undefined" errors resulting from the browser. On the other hand, having declared the variable, even with a zero value, the browser has a definition for it, and doesn't throw up. An ounce of tylenol is worth a pound of headache!
[
ElectronicsTheory.Com
]
This Course was written by Ray Dall © All Rights Reserved.
This page and all it's content Copyright, Trademarks, Intellectual Properties
and other legal issues 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006 Ray Dall.
All Rights Reserved.
And for what it's worth... this page was last updated HexDate 01-11--7D1
VISITORS: