Bookmark and Share

JavaScript Statements

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

Advertise on Tizag.com

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 pressing return and starting a new line.

Categories of Statements

In addition to 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. These distinct groups of statements include:

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

Conditional Statements

If you were to win a $100 million lottery, 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 (you didn't win the lottery), 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 and 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." With the right planning, looping will turn your cute little scripts into massive workhorses.

A loop statement checks to see if some condition is true, and if that condition is true, it executes a chunk of code. After the code is executed, the condition is checked again. If it is true, the process starts over again; if it is false, the loop stops and the rest of the code continues along. If we think about our wedding invitation example as a loop, we would first check if there are any invitations left. If there are, we would stuff, lick, and seal the next envelope. If there are no envelopes left, we would stop.

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 from being be read, you can then place in comments for yourself, much like HTML comments. 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.

Bookmark and Share




Found Something Wrong in this Lesson?

Report a Bug or Comment on This Lesson - Your input is what keeps Tizag improving with time!

Advertise Here

More Tutorials!
Microsoft Office Tutorials Artist Tutorials