Simple JavaScript Examples

The following is a table of numbers and their factorials. The JavaScript factorial function is defined in the <head> of this document, and called by JavaScript within this <table> element.

It would be better style to define the <table> completely within the JavaScript, rather than doing part of it in HTML and part in JavaScript. I did it this way to emphasize that JavaScript and HTML can be arbitrarily mixed together.

The following example is more complex. It uses an HTML <form> to input and output data, does some validity checking, and displays an alert box for invalid entries.

Enter a number:

The factorial is:

Remember to look at the source for this page!