Miscellaneous

  Home arrow Miscellaneous arrow Page 4 - Introduction to Java Script
MISCELLANEOUS

Introduction to Java Script
By: Codewalkers
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 3
    2004-02-08

    Table of Contents:
  • Introduction to Java Script
  • Variables
  • Prompts
  • If -Then Statements

  •  
     

    SEARCH CODEWALKERS

    TOOLS YOU CAN USE

    advertisement

    Introduction to Java Script - If -Then Statements


    (Page 4 of 4 )

    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.
    blog comments powered by Disqus

    MISCELLANEOUS ARTICLES

    - Oracle Database XE: Indexes and Sequences
    - Modifying Tables in Oracle Database XE
    - Oracle Database XE: Tables and Constraints
    - More on Oracle Databases and Datatypes
    - Oracle Database XE Datatypes: Datetime and L...
    - Oracle Database XE Datatypes: Character and ...
    - From Databases to Datatypes
    - Firefox 3.6.6 Released with Improved Plug-in...
    - Attention Bloggers: WordPress 3.0 Now Releas...
    - Reflection in PHP 5
    - Inheritance and Other Advanced OOP Features
    - Advanced OOP Features
    - Linux from Scratch V.6.6 Review
    - Linux Gaining in Strength
    - Install Slackware on Your Old PC


    © 2003-2012 by Developer Shed. All rights reserved. DS Cluster 4 - Follow our Sitemap