Friday, December 16, 2011

JavaScript Statements


JavaScript Statements

  • JavaScript is a sequence of statements to be executed by the browser.
  • JavaScript is case sensitive - therefore watch your capitalization closely when you write JavaScript statements, create or call variables, objects and functions.
·        A JavaScript statement is a command to a browser. The purpose of the command is to tell the browser what to do.
·        This JavaScript statement tells the browser to write "Hello World" to the web page:
             document.write("Hello World ");
·        It is normal to add a semicolon at the end of each executable statement. Most people think this is a good programming practice, and most often you will see this in JavaScript examples on the web.
·        The semicolon is optional (according to the JavaScript standard), and the browser is supposed to interpret the end of the line as the end of the statement. Because of this you will often see examples without the semicolon at the end.
·        Note: Using semicolons makes it possible to write multiple statements on one line.


JavaScript Code

  • JavaScript code (or just JavaScript) is a sequence of JavaScript statements.
  • Each statement is executed by the browser in the sequence they are written.
  • This example will write a heading and three paragraphs to a web page:

Example:

<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 second paragraph.</p>");
document.write("<p>This is third paragraph.</p>");
</script>

</body>
</html>

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.
  • This example will write a heading and three paragraphs to a web page:
  • The example below is not very useful. It just demonstrates the use of a block. Normally a block is used to group statements together in a function or in a condition (where a group of statements should be executed if a condition is met).

Example:

<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 second paragraph.</p>");
document.write("<p>This is third paragraph.</p>");
}
</script>

</body>
</html>
 

JavaScript Comments

Comments can be added to explain the JavaScript or can be used to make the code more readable.

1) JavaScript single-Line Comments

Single line comments start with //.
The following example uses single line comments to explain the code:

Example:

<html>
<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>
</html>

2) JavaScript Multi-Line Comments

Multi line comments start with /* and end with */.
The following example uses a multi line comment to explain the code:

Example:

 <html>
<body>

<script type="text/javascript">
/*
The code below will write
one heading and two paragraphs
*/
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>
</html>

Using Comments to Prevent Execution

In the following example the comment is used to prevent the execution of a single code line (can be suitable for debugging):

Example:

<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>
</html>



 In the following example the comment is used to prevent the execution of a code block (can be suitable for debugging):

Example:

<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>
</html>


Using Comments at the End of a Line

In the following example the comment is placed at the end of a code line:

Example:

<html>
<body>

<script type="text/javascript">
document.write("Hello"); // Write "Hello"
document.write(" World!"); // Write " World!"
</script>

</body>
</html>

 

No comments:

Post a Comment