JavaScript Class
Guidelines
Some things to know about JavaScript.
Ending Statements with a Semicolon?
With the traditional programming languages C++ and Java, each code statement
has to end with a semicolon.
Many programmers continue this habit when writing JavaScript, but in general,
semicolons are optional and are required only if you want to put more
than one statement on a single line.
How to Handle Older Browsers
Older browsers that do not support scripts will display the script as page
content. To prevent them from doing this, you can use the HTML comment tag:
The two forward slashes in front of the end of comment line (//) are a
JavaScript comment symbol, and prevent the JavaScript from trying to compile the
line.
Note that you can't put // in front of the first comment line (like
//<!--), because older browsers will display it. Funny? Yes! But that's the
way it is.
JavaScript is Case Sensitive
A function named "myfunction" is not the same as "myFunction". Therefore
watch your capitalization when you create or call variables, objects and
functions.
Symbols
Open symbols, like ( { [ " ', must have a matching closing symbol,
like ' " ] } ).
White Space
JavaScript ignores extra spaces. You can add white space to your script to
make it more readable. These two lines mean exactly the same:
name="Jeremiah"
name = "Jeremiah" |
Break up a Code Line
You can break up a code line within a text with a backslash. The
example below will be displayed properly:
document.write("Hello \
World!") |
Note: You can not break up a code line like this:
document.write \
("Hello World!") |
The example above will cause an error.
Insert Special Characters
You can insert special characters (like " ' ; &) with the backslash:
document.write ("You \& I sing \"Happy Birthday\".") |
The example above will produce this output:
You & I sing "Happy Birthday". |
Comments
You can add a comment to your JavaScript code starting the comment with two
slashes "//":
sum=a + b //calculating the sum |
You can also add a comment to the JavaScript code, starting the comment with
"/*" and ending it with "*/"
sum=a + b /*calculating the sum*/ |
Using "/*" and "*/" is the only way to create a multi-line comment:
/* This is a comment
block. It contains
several lines*/ |
|