ÄúµÄλÖãºÑ°ÃÎÍøÊ×Ò³£¾±à³ÌÀÖÔ°£¾JavaScript£¾Javascript Tutorial
Javascript Tutorial

Javascript - Intro
Javascript - Syntax
Javascript - Location
Javascript - External
Javascript - Operators
Javascript - Variables
Javascript - Functions
Javascript - Events
Javascript - Statements
Javascript - If
Javascript - Else If
Javascript - While
Javascript - For Loop
Javascript - Comments
Javascript - Array
Javascript - Alert
Javascript - Confirm
Javascript - Prompt
Javascript - Print
Javascript - Redirect
Javascript - Pop Up
Javascript - Date
Javascript - Form
Javascript - Void 0

Javascript String

Javascript - Strings
Javascript - Length
Javascript - Split
Javascript - Search
Javascript - Replace
Javascript - indexOf
Javascript - Compare

Javascript Advanced

Javascript - getElementById
Javascript - innerHTML

Javascript Statements

All the Javascript code that you will write will, for the most part, be comprised of many separate statements. A statement is setting a variable equal to a value. A statement is also a function call, i.e. document.write(). Statements define what the script will do and how it will be done.

Typical Ending of a Statement

In typical programming languages like C and PHP, the end of a statement is marked with a semicolon(;), but we have seen that the semicolon is optional in Javascript. In Javascript the end of a statement is most often marked by starting a new line.

Categories of Statements

In addition to the standard statements like changing a variable's value, assigning a new value, or calling a function, there are groups of statements that are distinct in their purpose. We will provide a brief overview of each of these categories in this lesson and cover them in greater detail later in the tutorial.

  • Conditional Statements
  • Loop Statements
  • Object Manipulation Statements
  • Comment Statements
  • Exception Handling Statements

Conditional Statements

If you were to win a $100 million lottery then you would probably quit your job. That last statement is a conditional if/then statement that is used a great deal in programming. If some condition (winning the lottery) is true, then something happens (you quit your job). If the condition is false, then you probably will not take that action (quit your job).

Conditional statements are used to control your scripts so that different actions can be taken depending on the situation. You may want to display a special image on your home page during the holidays. This condition would depend on what day it was, and if it was a holiday, then a special holiday image would be displayed to your visitors. Without proper knowledge of the conditional statement your scripts will not be as lively or dynamic as they could possibly be.

Loop Statements

Have you ever had to send out marriage announcements? If not, this is how it goes. You take the invitation, place it in the envelope, lick the envelope, seal the envelope, then send it off. Then you take the next invitation off the stack of 99 remaining invitations, place it in an envelope, lick the envelope, seal... You get the idea! It is a boring, repetitive task!

Wouldn't it be great if there was an easier way? Well in programming and in Javascript there is. The process is called "looping" and it will turn your cute little scripts into massive work horses with the right planning.

A loop statement checks to see if some condition is true, say our condition is "Are there any invitations left?" and if that condition is true it will execute a chunk of code. Our code in this example would be to stuff, lick, and seal the envelope. After the code is executed the condition is checked again, if it is true then the process begins again, if it is false, then the loop code stops execution and the script continues along.

Believe me when I say this is something you want to learn more about!

Object Manipulation Statements

These are statements that are used to take advantage of the object model to get tasks done. If you do not know about the object model at this time, do not worry. We will be talking about it later.

Comment Statements

Comment statements are used to prevent the browser from executing certain parts of code that you designate as non-code. Why would you want to do this? There are many reasons. By disallowing the block of text to be read you can then place in comments for yourself, much like HTML comments, and you can also block out segments of your code for whatever reason you may have.

The single line comment is just two slashes (//) and the multiple line comment starts with (/*) and ends with (*/). We will talk about comments in greater depth in a later lesson.

Exception Handling Statements

Sometimes when you are programming you do not know for sure if the file that you will be writing to, the input stream you are reading from, or the connection you have established will be usable for the code that you want to execute. There is a way to program safety mechanisms, so that your code handles common problems that may arise (maybe the input stream you are reading from suddenly disappears).

The try...catch statement tries to execute a piece of code and if it fails the catch should handle the error gracefully. This is an advanced programming subject that is interesting, but not necessary for the majority of Javascript programmers.

Wrapup

I hope you found this overview of Javascript statements interesting. Do not despair if you have not grasped all the details discussed above, as we will be covering them further in a later lesson.