Your Guide to Professional
Web Site Design and Development

HTML Tips
HTML Codes
Web Development
Web Design Tips
JavaScript Codes
216 Web Safe Colors
CSS Tutorial
JavaScript Tutorial
ASCII Character Codes

 

| Web Site Development | HTML Codes | HTML Tips | Web Design TipsJavascript Snippets | 216 Safe Colors | Symbols | CSS Tutorial | JavaScript Tutorial |

JavaScript Tutorial: JavaScript Statements


In JavaScript we use statements that tell the script to check to see if something is true - or present - or not. Common JavaScript statements are if, for, else, and else if. These statements are very important to JavaScript, and you will be using them a lot when you write your own JavaScript.

Let's first look at the 'if' statement. We use if to check for equalities. The if statement always includes the parentheses () after it, and what we are checking for goes inside the parentheses. We also use curly brackets {}with the if statement. The code that we want to run is inside the curly brackets.

So, what the script does is it looks to see if the condition that we have specified in the parentheses exists, and if it does, it runs the code inside the curly brackets that follow it. If the condition does not exist, it skips the code inside the curly brackets. Here is an example:


var a = 1;
var b = 2;
var c = 3;
if ((a != 0) || ( (b >= 2) && !(c > 9) ))
{
document.write("This is right.");
}


Now, you see on the if statement that we've used some characters: !, | |, &&, and >. These characters all mean something.

The ! character stands for NOT. In our example, it means if a is not equal to 0.

The | | characters means OR and the > character means greater than. Our example now reads if a is not equal to 0 or b is not greater than 2.

The && Characters mean AND. The statement now reads if a is not equal to 0 or b is greater than 2 and it is not true that c is greater than 9. Now, if this statement is true, or the condition exists, the code inside the curly brackets will run, and the words This is right will be printed on the web page. Using the ! character before a statement changes the meaning of the statement to mean that it is NOT true. In this example, c is greater than 9 is NOT true.

Pay special attention to how parentheses are used in the example. This is very important, and if it isn't done correctly, the script will not run.

Next is the else statement. Else would be used if the if statement is not true. If you will have more than two possible conditions, you would use if first, followed by else if, followed by else. You can have as many else if statements as you need.


var a = 0;
if (a > 0)
{ do something };

else if { do something else };

else { do this };


The JavaScript reads from top to bottom. If the if statement is true, it runs the code in the curly brackets, and ignores the else if and else statement that follows.


The for statement is used to perform a loop. This means that the script will run the same piece of code for however many times you specify. Here is an example:

for (var a=0; a<10; a++)
{ document.write("Test" + a); }


This statement says that as long as a is less than 10, the code inside the curly brackets should run. Furthermore, each time the code is run, it should be increased by 1. This is indicated with the double plus signs. You could also have it decrease by 1 each time by using a double minus sign, - -

Previous

Next



  JavaScript Tutorial: An Introduction to JavaScript
  The Basics of JavaScript
  JavaScript Events
  JavaScript Variables and Arrays
  JavaScript Statements
  JavaScript Functions and Methods
  JavaScript Errors
  JavaScript Food Chain
  Writing JavaScript Code
  JavaScript Object Reference
  JavaScript Events Reference
  JavaScript Functions Reference
  JavaScript Math Object Reference
  JavaScript Array Object Reference
  JavaScript String Object Reference
  JavaScript Date Object Reference

Web Development Tutorials

  Cascading Style Sheets Tutorial: An Introduction to Cascading Style Sheets
  JavaScript Tutorial: An Introduction to JavaScript
  Web Development: A step by step guide to developing a successful Internet business
  HTML Codes Chart: Copy and paste HTML codes for your web page
  HTML Tips: Copy and paste special effect HTML codes for your web page
  Web Design Tips: Tips, tricks, and special effect codes for your web page
  JavaScript Code Snippets: Copy and paste special effect JavaScript codes for your web page
  216 Web Safe Color Chart: Hexadecimal and RGB Color Codes for your web page
  ASCII Character Codes Chart: American Standard Code for Information Interchange character codes chart