Section 16.1. JavaScript Syntax

[Page 529 (continued)]

16.1. JavaScript Syntax

We call the look of a programming language its syntax. Java is a fairly traditional programming language in terms of its syntax. It doesn't look much different than C or C++ or other languages. Java was designed to make it easy for C or C++ programmers to learn.

Languages like Scheme and Smalltalk look much more different. There are several things that you should expect to be different and that you'll need to explore when comparing the syntax (look) of different languages:

  • How do you define variables? In many languages, you must declare the variables and their types (e.g., floating point vs. integer vs. string vs. list) before you can assign them.

  • How is each line constructed? In languages like Java and C, each line must end with a semicolon.

  • [Page 530]
  • How are blocks defined? How do you group statements for loops, conditionals, and function bodies?

  • How do you define functions, procedures, methods, classes, and so on?

JavaScript is meant to be a scripting language. This means that it's meant to be used easily, by non-professional programmers, to solve relatively simple tasks. No one is going to use JavaScript to write the master calculation program for the IRS, nor something that tracks all the accounts for owners of MasterCards. It's designed to look a lot like Java (and C), to make it easier to pick up for people familiar with those kinds of languages.

JavaScript can be used to program the Web server or interact with the viewer of the Web page on the client. We're going to emphasize the latter. When JavaScript is used on a client machine, it's actually executed by the browseryour browser contains a JavaScript interpreter. We call this client-side JavaScript.

In JavaScript, variables are supposed to be declared before use, but you don't have to specify their type. The type will be determined based on how you use it. However, JavaScript isn't strict about thisyou can simply say a = 12;. But it's more correct to declare the variable as something you'll be using by either typing something like var a = 12; or:

var a; ... a = 12;

Like Java, JavaScript defines blocks using curly braces. You can use any indentation you want, but the block begins with a curly brace and ends with a close curly brace.

function test() {     document.writeln("This is a test"); }

In Javascript you can define functions which are like Java methods. The above code defines a function named test that doesn't take any parameters and writes to the HTML document the string This is a test.

We'll find many similarities and differences between Java and JavaScript when we get down to writing individual lines.

  • Like Java, JavaScript ends each line with a semicolon (;). You can break up lines so that they are readable to you, as long as each ends with a semicolon.

    var myvariable = (4 * x) +                  (5 * y);

  • JavaScript's for is the same as Java's for loop. It is used when you know how many times a loop will be executed. After the keyword for comes a parenthesized list of three expressions separated by semicolons. The first expression is evaluated before the for loop begins. The second expression is evaluated at the end of the for loop to see if we continue loopingif the expression is true, we keep looping. The third expression is what to do before repeating the loop. We'll see a for loop later in this chapter.

    [Page 531]
  • In JavaScript, we use write and writeln instead of Java's System.out.print and System.out.println, and instead of writing to a Command Area, we'll actually be writing to the HTML page itself!

But in a real sense, these are just details. The basic operation of JavaScript is not unlike Java. Statements are still executed one after the other, there are still variables, loops, and conditionals, and we'll still have functions and methods. All of what you knew before still applies.

JavaScript is all about objects. Just about every function is actually a method. There isn't a global print, and there's not even a global write or writeln. Instead, to write into the document, we'll use document.write() (or, to end the line with a newline, document.writeln()). The document here is the HTML document itself.

Introduction to Computing & Programming Algebra in Java(c) A Multimedia Approach
Introduction to Computing & Programming Algebra in Java(c) A Multimedia Approach
Year: 2007
Pages: 191 © 2008-2017.
If you may any questions please contact us: