JavaScript Chapter 02 - 927F3F
JavaScript Chapter 02 - 927F3F
NOTES: Using semicolons makes it possible to write multiple statements on one line, although good programming practice encourages placing only one statement per line.
JavaScript Code
JavaScript code (or just JavaScript) is a sequence of JavaScript statements. Each statement is executed by the browser in the sequence it is written.
<html> <body> <script type="text/javascript"> document.write("<h1>This is a heading</h1>"); document.write("<p>This is a paragraph.</p>"); document.write("<p>This is another paragraph.</p>"); </script> </body>
JavaScript Blocks
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.
<htm1> <body> <script type="text/javascript"> { document.write("<h1>This is a heading</h1>"); document.write("<p>This is a paragraph.</p>"); document.write("<p>This is another paragraph.</p>"); } </script> </body> </htm1>
JavaScript Comments
JavaScript comments can be added to explain the JavaScript script or to make the code more readable. Single line comments start with //. The following example uses single-line comments to explain the code.
<htm1> <body> <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> </body> </htm1>
<htm1> <body> <script type="text/javascript"> { /* The code below will write one heading and two paragraphs */ document.write("<p>This is a paragraph.</p>"); document.write("<p>This is another paragraph.</p>"); } </script> </body> </htm1>
<htm1> <body> <script type="text/javascript"> { //document.write("<p>This is a paragraph.</p>"); document.write("<p>This is another paragraph.</p>"); } </script> </body> </htm1>