As you've read the section about HTML (if you haven't done so please go back to the Programming section and visit the HTML subsection) I can start the one about JavaScript: created in 1995 by Netscape, JavaScript has been updated by the JScript by Microsoft and ECMAScript by ECMA and has reached its version 1.6 in 2005. It's a programming language that you can directly embed into classic HTML.


So, we shall start by the most important question: what's a program? A program is a list of orders that are executed by someone. In our case, the orders are called instructions and they're executed by a computer. For exemple, the JavaScript program window.alert("hello") will alert, in a window, the message hello (you can try it out by clicking on it). A programming language is used to define programs. In JavaScript, programs are executed in the order they're written, so the program alert(1);alert(2) will alert 1 and then alert 2.


Programming language generally share some bases:

Variables: a variable is a name that we give to a value. To give values to variables, different languages use different syntaxes and JavaScript uses the = symbol. For exemple, to alert like hello john, we can write the following program (that you can click on to execute): variable1="hello";variable2="john";space=" ";window.alert(variable1+space+variable2). WARNING: the = of JavaScript is not the same thing as a mathematical equality, as a proof the i=i+1 is a perfectly valid JavaScript instruction that gives the value i+1 to the variable i whereas the mathematical equality i=i+1 is trivially incorrect. A good programmer always gives logical names to variables (therefore names that remind of their content).

Functions: a function is a name that we give to a list of instructions. For example, window.alert is a JavaScript function. In JavaScript a programmer can create its own functions using the function keyword, for example the function function multiply_by_two( number ){ return 2 * number } multiplies by two and returns a given number. Functions are important as they let us write programs that are less cluttered and much easier to read. To call that function we can simply write, for example, variable = multiply_by_two( 2 ) in which case the variable variable will store as value 2 * 2, therefore 4. In all programming languages, functions are inactive (therefore not executed) as long as they're not called by someone.


By now, we know how to calculate some things, remember values and remember some list of instructions. Yet, to have useful programs, we must have two more things:

Conditionals: a conditional instruction is an instruction that's executed if and only if a certain condition is true. For example, let's suppose that the day_of_week tells about the day of the week. The program if( day_of_week() == "Wednesday" ){ alert("it's wednesday") }else{ alert("it's not wednesday" ) } will alert it's wednesday when launched a wednesday, and it's not wednesdsay when launched any other day.

Loops: a loop is executed over, and over, and over again each time a condition is true. JavaScript has two loop forms: for and while. The program for( i = 0 ; i < 10 ; i = i + 1 ){ alert(i) } will alert each number between 0 and 10. Similiarly, the program Similarly, the program while( day_of_week() == "Wednesday" ){ alert("it's still wednesday!") } will keep on alerting it's still wednesday! during the whole day of wednesday and stop on thursday.


Those four notions exist in any "useful" programming language, including JavaScript. The JavaScript is inserted into HTML code using the <script> and </script> tags. For example, you can write <script>document.write("hello")</script> into our famous test.html of our HTML lessons and observe that it now displays hello in the web browser's window. You'll also note that it doesn't display the JavaScript code! You can also try, for example, to alert <script>window.alert("hello")</script> or even to mix HTML code and JavaScript code on the same page (it should work perfectly).

A big difference between JavaScript and other programming languages is that JavaScript is an interpreted language, therefore interpreted and executed command by command at each time. The interpreter therefore remembers all things like variable names, and to take advantage of that JavaScript has the eval function: this function evaluates (executes) a given statement. You can try it out using eval("window.alert('hello')") or even using eval("i"+number) that'll have as value, for example, the value of the variable i5 if nombre equals 5. This process (dynamic native code generation) cannot be done in most other programming languages.

On the other hand, since interpreted, JavaScript is way slower that most other programming languages.


Of course, I cannot give you an entire JavaScript lesson in just one simple text, still I recommend your reading of the article about DHTML to understand more and start doing some exercices afterwards.