Javascript Statements

Statements in JavaScript are similar to the English language in their structure. JavaScript statements end with a semicolon just as sentences end with a period in English. English is read from left to right, and top to bottom. Browsers also read code from left to right and top to bottom. Below is an example.

<script language="JavaScript"> <!-- Hide JavaScript code from old browsers var country = "USA"; document.writeln("My name is John.<br>"); document.writeln("I live in the" + country + ".<br>"); // end hiding code --> </script>

In the example above a variable named country is declared that holds the string USA. The document.writeln statement tells the browser to print on the screen what is contained inside the parentheses.

You should recognize the HTML tag <br>. HTML tags are permitted inside writeln statements, and perform just as they would in any HTML document. So, in this example, each writeln statement is output to a different line. One thing to remember when adding HTML tags is that they should be placed inside quotes like a string. It doesn't matter if you use single or double quotes, the browser will treat them in the same fashion.

The second writeln statement will output the variable country. The variable should not have quotes around it. If you use quotes, the word country will be printed and not the value of the variable, which is USA.

Intro         • Variables         • Statements         • If then else

Loops         • Objects         • Functions

Home   |   Business Directory   |   Festivals and Fairs (USA)