================================================

JavaScript Part Two.

Last week I introduced some of the basic concepts you will
need to know in order to write your own
JavaScript
programs.
JavaScript is probably one of the easiest
programming languages to learn and well worth the effort
if you are interested in adding dynamic content to your
web site. This week I will continue with this topic. Be
sure to read or review last weeks article first. It is
available at: www.imswebtips.com/issue65top1.htm.

From last weeks article you should have some idea of the
syntax used by
JavaScript. Syntax is important since bad
syntax will be the most common cause of errors in your
program. But what is "Syntax". Syntax simply means the
format of the code. It is the computer program's
equivalent of spelling and grammar. Whereas people can
often interpret the meaning from text that includes
spelling and grammatical errors, computers simply cannot
interpret the correct meaning from a line of code that
contains syntax errors.

What are some of the most common syntax error?

Spelling and Grammar.

This code makes sense:

<SCRIPT language="
JavaScript">
var
int1 = 5;
var
int2 = int1 + 1;
document.write("int1=" + int1 + " int2=" + int2);
</script>

is66fig1.htm

This code does not:

<SCRIPT language="JavaScript">
var int1 = 5;
var int2 = Int1 + 1:
document write("int1=" + int 1 + " int 2= + in2);
</script>

is66fig2

Can you spot the errors?

Line 3: 'Int1' has a capital "I" but the variable 'int1' is declared with a lowercase "i".
Line 3: Is terminated with a colon ":" but should be terminated with a semi-colon ";"
Line 4: 'document write' is missing a period ".". It should be '
document.write'.
Line 4: '
int 1' has a space. It should be 'int1'.
Line 4: The second string "
int2= is missing its closing quote. It should be "int2=".
Line 4: The final variable '
in2' is misspelled. It was declared as 'int2'.

Any one of these problems will prevent this program from
running. As you get the hang of programming, these types
of syntax problems will only be a minor annoyance. Even
the most experienced programmers make them all the time.
Quite often we let the computer find them for us instead
of trying to check each line ourselves.

There you go, "syntax" is not all that hard a word to
understand. When you start programming you will use it
quite a lot.

The program above contains some new code that I have not
shown you before.

var
int1 = 5;
var
int2 = int1 + 1;

This is a little bit of math. "Math" is another word that
tends to send many people running for cover. Programs
including
JavaScript can perform extremely complex math
fast and effectively. Unless you are a mathematician with
a need to perform complex math, you will not have any need
to. The vast majority of programs only contain very simple
math. Depending upon what you are doing, your programs may
not need to contain any math at all.

Even so, several mathematical operations are quite common
and knowing how to perform a little bit of addition and
subtraction can be quite useful. Fortunately when there is
some math that needs to be done, it will be the computer
that does it. All you need to do it tell it what you want
doing.

Take a look at these two lines. They both start with
"var". "var" is a declaration that means that the next
word is a variable or a word that can contain any value
you want to give it. The variable '
int1' is assigned a
value of '5'. The variable '
int2' is assigned a value
equal to whatever '
int1' equals plus 1. Since 'int1' has
been assigned a value of '5', the statement:
Var
int2 = int1 + 1; is exactly the same as:
Var
int2 = 5 + 1; or
Var
int2 = 6;

How can you be sure?

The write operation:
document.write("int1=" + int1 + " int2=" + int2);

If you revisit issue65top1 you will see that it prints:
"int1=5 int2=6"

This write operation adds two constants and two variables.
The first constant is the string "int1=". It is a constant
because it cannot be changed so it is printed exactly as
is. Following this constant is the variable 'int1' that we
have assigned the value '5'. This is followed by the next
constant " int2=" and the second variable 'int2' which now
equals '6'. When these constants and variables are added
together with "+" operations, it makes a text string:
"
int1=5 int2=6".

The print statement illustrates how it is possible to add
strings together. Take a look at this example:

<SCRIPT language="JavaScript">
var int1 = "Hello";
var int2 = " World";
document.write(int1 + int2);
</script>

is66fig2.htm

This example is almost the same as the previous examples
except that the variables "int1" and "int2" have been
assign text string values instead of integer values. Now
when the write statement adds int1 and int2 it creates a
text string that says "Hello World".

In JavaScript unlike other programming languages, it is
possible to assign different types of variables using the
"var" statement. Examples are:
var svar = "Hello"
var ivar = 10;
var fvar = 20.5;

Performing operations using a "+" operation will add
numbers but combine strings into longer strings. It does
not make much sense to perform other mathematical
operations such as Multiply *, Subtract - , Divide /, etc.
on strings.

Next week I will look at manipulating and using variables
to create a simple Dynamic HTML animation.


================================================
"IMS Web Tips" ISSN 1488-7088
© Copyright
2000 Virtual Mechanics
================================================

"IMS Web Tips" is a weekly news letter for all web site managers regardless of experience who are looking for detailed information on creating, maintaining and promoting their web sites.

To subscribe send a
blank e-mail to join.imswebtips@list.imswebtips.com  or
visit the IMS Web Tips home page for subscription information and a list of past articles.

---------------------------------------------------------------------

If you like the contents of this newsletter, please recommend it to a friend. Not only will you help us to continue to provide you with useful and informative articles, you could also win $10,000. Click here for details.

Click here for a free WYSIWYG pixel precision HTML editor.
Join the IMS Web Tips weekly newsletter
Email: Name:

Please recommend us to a friend.
Home         Archive by Date         Archive by Topic        Virtual Mechanics