JavaScript statements can be grouped together in blocks.
Blocks start with a left curly bracket {, and end with a right curly bracket }.
The purpose of a block is to make the sequence of statements execute together.
An example of statements grouped togheter in blocks, are JavaScript functions.
This example will run a function that will manipulate two HTML elements:
Example
function myFunction()
{
document.getElementById("demo").innerHTML="Hello Dolly";
document.getElementById("myDIV").innerHTML="How are you?";
}
Try it yourself »
You will learn more about functions and conditions in later chapters.
JavaScript comments can be used to make the code more readable.
JavaScript Comments
Comments can be added to explain the JavaScript, or to make the code more readable.
Single line comments start with //.
The following example uses single line comments to explain the code:
Example
<script type="text/javascript">
// Write a heading
document.write("<h1>This is a heading</h1>");
// Write two paragraphs:
document.write("<p>This is a paragraph.</p>");
document.write("<p>This is another paragraph.</p>");
</script>
Try it yourself »