// code goes here.
// Just start with a double slash.
Oddly, you have to declare a variable with the “var” command the first time you use it, but do not have to cast it as a type.
alert("I\'m another annoying pop-up message!");
document.write("Welcome to my web page!");
There’s various ways you can output messages, since it’s a web browser environment.
Not as straight-forward as with other languages. You have to temporarily drop into HTML:
<form action="receiving_URL" >
<input type="text" name="form_data" value="enter text here" />
<input type="submit" value="Submit" />
document.write(Math.pow(2, 8) + "<br />");
“Math.pow” calls the Math module, and ‘pow’ is the “power of” function. We call it this way without having to include the Math module.
if ((this_year % 4) == 0)
Pretty much like C. Switch statements are also supported:
document.write("You have no special powers. <br />");
document.write("You may moderate. <br />");
document.write("You may edit other\'s comments. <br />");
document.write("Dude! You have Magic Missile! <br />");
var limit = new Array(1, 2, 3, 4, 5);
for (count in limit)
document.write("Number " + count + "<br />");
Note, by the way, that we use HTML break tags to designate new lines, just like we would add “\n” newline escapes in C or Python.
document.write("The number is " + i);
With an HTML tag:
<script src="/path/to/my/script.js" />
Typically, you would declare functions in the head of the HTML document, to ensure that they are read before they are called. Then call them from the body. An example of how the whole set-up looks:
document.write(doubler(10) + "<br />");
To round out our tour, we will visit another very powerful language which also lives mostly on the web… in our next installment.
- The PHP Programming Language
- A Fast Round-Up of Web Languages
- The Python Programming Language
- Shell Programming
- The C Language