|
JavaScript is a sequence of statements to be executed by the browser. 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 noticeable 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 If you were to win a $100 million lottery then you would most likely quit your job. That last statement is a conditional if/then statement that is used a great deal in programming. If some situation (winning the lottery) is true, then something happens (you quit your work) 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 events 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 information 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 outstanding 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 considerable 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 recognized will be functional for the code that you would like to execute. There is a way to program security 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 needed for the majority of Javascript programmers. Wrapup I hope you found this overview of Javascript statements interesting. Do not despondency if you have not grasped all the details discussed above, as we will be covering them further in a later lesson. JavaScript Statements Example A JavaScript statements is a command to the browser. The purpose of the command is to tell the browser what to do. This JavaScript statement tells the browser to write "Hello" to the web page: document.write("Hello"); 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.
|