Exploring JavaScript
The way to really learn a new programming language is to write programs with it. As you read through this book, I encourage you to try out JavaScript features as you learn about them. There are a number of techniques that make it easy to experiment with JavaScript.
The most obvious way to explore JavaScript is to write simple scripts. One of the nice things about client-side JavaScript is that anyone with a web browser and a simple text editor has a complete development environment; there is no need to buy or download special-purpose software in order to begin writing JavaScript scripts. We saw an example that computed factorials at the beginning of this chapter. Suppose you wanted to modify it as follows to display Fibonacci numbers instead:
<script>
document.write("<h2>Table of Fibonacci Numbers</h2>");
for (i=0, j=1, k=0, fib =0; i<50; i++, fib=j+k, j=k, k=fib){
document.write("Fibonacci (" + i + ") = " + fib);
document.write("<br>");
}
</script>
This code may be convoluted (and
don’t worry if you don’t yet understand it), but the
point is that when you want to experiment with short programs like
this, you can simply type them up and try them out in your web
browser using a local file: URL. Note that the
code uses the document.write( )
method to display its HTML
output, so that you can see the results of
its computations. This is an important technique for experimenting
with JavaScript. As an alternative, you can also use the
alert( ) method to ...