previous

page 16

next

I want to introduce some very important new concepts.
To keep it simple, we'll look at these by themselves.
Create a new file called play1.html for want of a better name.

We can get JavaScript to write directly to a blank web page. Try this:

<script>
document.write('Hello World');
</script>

You should just see the words Hello World on your browser screen.

You can even use html tags!

<script>
document.write('<h1>Hello World</h1>');
</script>
The for loop

One of the most powerful ideas in computing is the loop.
This is where we can tell a computer to do something hundreds (maybe billions) of times - computers don't get bored! Try this:

<script>
for (var i=0; i<1000; i++) {
document.write(i + ' ');
}
</script>

Impressive eh? The ++ is shorthand for add one.
What this for loop does is to use a variable i to count from 0 to 999.
Note that the < before the 1000 means less than.
The browser doesn't get confused because it knows this is JavaScript because of the script tags.

previous
next