If then statements are one of the simplest tools that someone can use in programming to account for multiple options. These statements do exactly what they say. IF some condition is true, THEN do something. The if-then statement is common to all programming languages. While each is formatted a little differently, they all do the same thing. In JavaScript the format for if-then statements is as follows:
if (some condition is true)
{
do something;
do something;
do something;
}
There are some rules to note here. First, the if must be lowercase. Second, the rules of the if-then statement must be contained in the parenthesis. Third, the actions that are to be carried out must be in the curly brackets. Let’s try to use an if-then statement in conjunction with our variables. Follow the example below and then try it yourself.
<Script Language="JavaScript">
<!--Hide me from the dumb browsers
/* here is my variable declaration section */
var apple=prompt("Do you like apple pie and vanilla ice cream?","yes or no");
if (apple == "yes")
{ alert("That's great! Me too! Would you like to get some?"); }
if (apple=="no")
{ alert("What? Are you nuts? Apple pie and vanilla ice cream is the greatest!"); }
// stop hiding me --> </script>
You should see something new here. Most notably the alert function. This function simply puts a little alert box on your browser. It can be novel, but should be carefully considered before using as many people can find it annoying. Now lets look at a couple of items. When setting the if value to any condition you must use two equal signs ( ==), not just one. This must be remembered or else your if then statements will not work. Also, notice that I have used two separate if-then statements. While there are easier ways to do this, which we will cover later, it is important to see the long way in order to properly grasp the concept. One of the problems with the this format is that the person using the JavaScript, must enter the possible options exactly, or else the JavaScript will not work. Later in this course, we will look at using wild cards to try and open the options up a bit.
There are other options that you can use to make your if then statements more refined. They are simply listed below. You should review them and try them out to see what combinations produce what results.
< - Less than > - Greater than <= - Less than or equal to >= - Greater then or equal to != – not equal to && - and || - either condition may be true
Here are examples of each.
//If variable 1 is less than variable 2 then if (var_1 < var_2) then //If variable 1 is greater than variable 2 then if (var_1 > var_2) then //If variable 1 is less than or equal to variable 2 then if (var_1 <= var_2) then //If variable 1 is greater then or equal to variable 2 then if (var_1 >= var_2) then //If variable 1 is not equal to var_2 then if (var_1 != var_2) then //If variable 1 is less than variable 2 //and variable 1 is not equal to variable 2 then if(var_1 < var_2) && (var_1 != var_2) then //If variable 1 is less than variable 2 //or variable 1 is not variable 2 then if(var_1 < var_2) || (var_1 != var_2) then
About the Author
John Johnston lives in North Pole Alaska and works for the Fairbanks North Star Borough Public Libraries as a network administrator. He has written a number of tutorials on basic computer introduction and maintenance. His hobbies include river running, snowmobiling, hunting and just getting lost in the Alaskan Wilderness.
DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.