Basic JavaScript Notes

Don't use document.write

Some of the examples in our textbook, Fundamentals of Web Development, use the document.write function to add information to an HTML document. Using document.write is okay for small examples like that, but you should not use it in your assignment or project code. Instead, use DOM (Document Object Model) functions or the innerHTML property. You can read about using the innerHTML property in the Generating HTML section below.

JavaScript debugging tutorial

Here is a tutorial on debugging JavaScript in a web browser. The tutorial includes a short video demonstration of using the Chrome developer tools.

Variables

Here are some important things to know about variables in JavaScript:

Strings

Strings in JavaScript are immutable. That means once a string is created, it can't be changed.

Generating HTML

You can see an example of generating HTML with a detailed explanation on these web pages:
genSample.html A web page with ten h2 headers generated by JavaScript code.
genSample.js The JavaScript code that generates the HTML for genSample.html.
genSampleNotes.html A walk-through of genSample.html and genSample.js

Using JavaScript outside a web browser

The most common way to execute a JavaScript program is inside a web browser. However, there are other ways to execute JavaScript programs, including server-side scripting in Node.js. Other server-side options are Netscape's web server and Microsoft's IIS web server.

Mozilla provides two JavaScript interpreters--Spider Monkey, which is written in C, and Rhino, which is written in Java. These interpreters can be embedded in other programs, which makes an easy way to add a macro or scripting language to another program. In fact, Version 6.0 of Java includes a JavaScript interpreter, so that any Java 6.0 program can execute JavaScript.