JavaScript/Introduction

From Wikibooks, the open-content textbooks collection

< JavaScript
Jump to: navigation, search
Previous: Contents Index Next: Javascript Tutorial


HTML is the core of a Web page. HTML, by its very nature, is static. All of its elements (<body>, <p>, and so on) cannot change once the page has been parsed (loaded and rendered). JavaScript can make your pages dynamic and interactive.

JavaScript is an object-oriented, interpreted language, with a syntax similar to C, C++ and Java. It is derived from C, as are C++ and Java, but is meant to be usable by non-programmers.

JavaScript is usually used for interacting and modifying pages after it has loaded (client-side): for example, a clock displayed on a page that updates itself to show the current time on the user's computer. Server-side languages, such as PHP, or Perl are the best way to modify a page before loading, although there are some cases of Server-JavaScript (SJS). Not all browsers have a JavaScript interpreter (such as the text-only browser Lynx), or runs the latest version. Furthermore, some users turn off JavaScript capabilities by choice. Generally, web pages should use JavaScript to enhance the user's experience, rather than depend on it.

[edit] Relation to Java

Despite the similar names, Java and JavaScript, there is almost no relation between the two languages. Since both are frequently used on the Internet the two can be confused by the uninitiated. In fact they were developed by two totally different companies, with differing goals and purposes in mind. Netscape developed JavaScript and Sun Microsystems developed Java. JavaScript can be interpreted by most browsers directly and quickly, while Java requires a separate "Java Virtual Machine" to be started before running. JavaScript and Java use a similar syntax (based on the C language) but many of the commands are quite different.

For example, compare the two methods of writing to the display (page). In Java, to write "Hello world!", the code would read:

System.out.println("Hello world!");

while in JavaScript (in a browser), it would say:

document.write("Hello world!");
Previous: Contents Index Next: First Program
Personal tools
Create a book
  • Add wiki page
  • Collections help
In other languages