Java Reference
In-Depth Information
3.
Put kettle on to boil.
4.
Has the kettle boiled? If so, then pour water into cup; otherwise, continue to wait.
5.
Drink coffee.
You'd start running this code from the fi rst line (instruction 1), and then continue to the next (instruc-
tion 2), then the next, and so on until you came to the end. This is pretty much how most computer
languages work, JavaScript included. However, there are occasions when you might change the fl ow of
execution or even skip over some code, but you'll see more of this in Chapter 3.
JavaScript is an interpreted language rather than a compiled language. What is meant by the terms
interpreted and compiled ?
Well, to let you in on a secret, your computer doesn't really understand JavaScript at all. It needs some-
thing to interpret the JavaScript code and convert it into something that it understands; hence it is an
interpreted language . Computers understand only machine code , which is essentially a string of binary
numbers (that is, a string of zeros and ones). As the browser goes through the JavaScript, it passes it to a
special program called an interpreter , which converts the JavaScript to the machine code your computer
understands. It's a bit like having a translator translate English to Spanish, for example. The impor-
tant point to note is that the conversion of the JavaScript happens at the time the code is run; it has to
be repeated every time this happens. JavaScript is not the only interpreted language; there are others,
including VBScript.
The alternative compiled language is one in which the program code is converted to machine code before
it's actually run, and this conversion has to be done only once. The programmer uses a compiler to convert
the code that he wrote to machine code, and this machine code is run by the program's user. Compiled
languages include Visual Basic and C++. Using a real-world analogy, it's a bit like having a Spanish
translator verbally tell you in English what a Spanish document says. Unless you change the document,
you can use it without retranslation as much as you like.
Perhaps this is a good point to dispel a widespread myth: JavaScript is not the script version of the Java
language. In fact, although they share the same name, that's virtually all they do share. Particularly
good news is that JavaScript is much, much easier to learn and use than Java. In fact, languages like
JavaScript are the easiest of all languages to learn, but they are still surprisingly powerful.
JavaScript and the Web
For most of this topic you'll look at JavaScript code that runs inside a web page loaded into a browser.
All you need in order to create these web pages is a text editor — for example, Windows Notepad —
and a web browser, such as Firefox or Internet Explorer, with which you can view your pages. These
browsers come equipped with JavaScript interpreters.
In fact, the JavaScript language fi rst became available in the web browser Netscape Navigator 2. Initially,
it was called LiveScript. However, because Java was the hot technology of the time, Netscape decided
that JavaScript sounded more exciting. When JavaScript really took off, Microsoft decided to add its
own brand of JavaScript, called JScript, to Internet Explorer. Since then, Netscape, Microsoft, and oth-
ers have released improved versions and included them in their latest browsers. Although these dif-
ferent brands and versions of JavaScript have much in common, there are enough differences to cause
problems if you're not careful. Initially you'll be creating code that'll work with most browsers, whether
Search WWH ::




Custom Search