The JavaScript Diaries: Part 1

By Lee Underwood

An Introduction

First, there are a few things you need to understand.

JavaScript is not Java
Java is a compiled language. This means that the Java code must be transformed ("compiled") into a high-level programming language before it can run.

JavaScript is an interpreted language. It doesn't need to be compiled before it is run. In an interpreted language the instructions are parsed (divided into small components that can be analyzed). For instance, as the browser "reads" this page, it breaks down each of the page's components into individual components and interprets each component as it moves down the page. In linguistics it means to divide language into small components that can be analyzed. For example, parsing this sentence would involve dividing it into words and phrases and identifying the type of each component (such as a verb, adjective, or noun).

JavaScript can either be client-side or server-side
This refers to the place where the script is processed. We will be dealing with client-side scripts. This means that the script is processed ("runs") in the client (e.g., browser). The script will be processed directly in the browser without having to send or receive data to or from the server. Most JavaScript is client-side.

JavaScript is an object-based scripting language
Object-based means that JavaScript sees the existing data structure as objects. A JavaScript object is merely a thing. It's like the objects around us. In addition, each object has properties. For example, I have two cats. They are the objects. They have fur, ears, and tails. These are the properties (more on this later.)

JavaScript is cross-platform
The code will run on a wide variety of computer platforms. This means that users with all kinds of computers using different kinds of operating systems are able to obtain the same results as everybody else.

JavaScript comes in different 'flavors'
  • JavaScript: This was the name given by Netscape, who invented it. It was first used in Netscape 2.0. This is the most popular version of the language and is what we will be learning. The current version is 1.5.
  • JScript: This is the scripting language invented by Microsoft to compete with JavaScript.
  • ECMAScript: The de facto international standard for JavaScript. This standard covers the core of the language.
Since version 4 of the Netscape and Internet Explorer browsers, JavaScript and JScript have had the same core functions. This means that they both utilize the core language features included in the EMCA standard. While IE recognizes both JScript and JavaScript, Netscape does not. However, since the tags are essentially the same, if the scripts are written in JavaScript, they will run on both browsers, for the most part. There are some differences between the JavaScript and JScript codes, but later on, we'll look at ways to overcome that within the context of the script itself.

A Few Things You Need to Remember

When writing scripts it's best to get in the habit of entering a semicolon at the end of each statement (e.g. var x = range;). While it's not required, it can help to eliminate errors.

There is no "proper" format for writing scripts. However, breaking up lines and indenting some of them does make it easier to read and debug. For instance, the layout here:

function getObject(obj) {
  var theObj;
  if(document.all) {
    if(typeof obj=="string") {
      return document.all(obj);
    } else {
  if(document.getElementById) {
    if(typeof obj=="string") {
      return document.getElementById(obj);
    } else {
  return null;

is much easier to read than this one:

function getObject(obj) {var theObj;
if(document.all) {if(typeof obj=="string") {return document.all(obj);}
else {return;}}
if(document.getElementById) {if(typeof obj=="string") {return document.getElementById(obj);}
else {return;}}return null;}

You also may need to edit a script several months after writing it. If it's cleanly formatted it will be an easier job. In addition, if someone else uses your script it will make it easier for them to understand how the script works. For the most part, JavaScript ignores tabs and spaces. A little later on in our study we will look at when it is important to pay attention to these "whitespaces."

It's also a good idea to add comments to your scripts, especially if the script is long. There are two methods for adding comments. For a single comment line use two forward slashes, "//". You can even do this on the same line as the code:

// This whole line will be ignored by the JS interpreter

var myOldCar = Ford     // The first part will be interpreted; this part will be ignored.

If you need to comment out several lines, you would begin with a forward slash and an asterisk, "/*", and end with an asterisk and a forward slash, "*/". For example,

/* All of this, all three lines,
will be ignored in their entirety
by the JavaScript interpreter */

Just a couple of cautions when placing comments in a script. Make sure you don't nest comments (one inside of another) and keep the comments concise to save download time. Everything you write in a script is downloaded to the browser. Although the comments are not viewed by the user, they still take up precious bandwidth and RAM.

Also, remember that JavaScript is case-sensitive. This means that it sees myAuto and myauto as two completely different variables. Be very careful when entering code. Using the wrong case will cause your script to fail.

Page 2 of 3

Previous Page
1 2 3
Next Page

Make a Comment

Loading Comments...

  • Web Development Newsletter Signup

    Invalid email
    You have successfuly registered to our newsletter.
Thanks for your registration, follow us on our social networks to keep up-to-date