SoFunction
Updated on 2025-04-13

JavaScript Basic Tutorial (Lesson 2) Page 4/7


The application of the "if" clause can cause the program to react differently according to the value entered by the user. For example, you could write a program that makes it react differently to you than to others. Here is its basic format:
    if (some condition is true)
    { 
        do something;
        do something;
        do something; 
    }
Important parts of this structure:
Start with the word "if" (if must be lowercase).
In parentheses, there are conditions: either true or false.
If the condition is true, execute the statement in the brace.
Remember: spaces are the only thing that keeps the program readable. Of course you can write the entire if statement in one line, but it's a bit harder to read.
Here is an example of an if clause.
    <script language="JavaScript">
    <!-- hide me
var monkey_love = prompt("Do you like net monkeys?","Click in yes or no.");
if (monkey_love == "Yes")
    {
alert("Thank you! I'm glad you're here! Please read it down!");
    }
    // end hide -->
    </script>
If you type yes in the live dialog box, you will receive a kind greeting. If you knock in something else, there is no.
Here is the core of this statement:
var monkey_love = prompt("Do you like net monkeys?","Click in yes or no.");
if (monkey_love == "Yes")
    {
alert("Thank you! I'm glad you're here! Please read it down!");
    }
You have seen the first line. It evokes a dialog box and calls the user's feedback into the variable monkey_love. But the second line is a little different: it has a condition, that is, if the variable monkey_love is equal to the value "Yes", then run the statement in curly braces. If it is equal to other values, it will not run.
Note that two of the conditions are equal to the mark, which is one of the things that people tend to get confused. If you only use one tag, it is actually telling JavaScript to test whether monkey_love is equal to "yes". Fortunately, most browsers recognize these errors and warn you when you run these statements. But it is best to start now and be careful not to make such mistakes.
Other important conditions are:
    (variable_1 > variable_2)  is true if variable_1 is greater than variable_2
    (variable_1 < variable_2)  is true if variable_1 is less than variable_2
    (variable_2 <= variable_2)  is true if variable_1 is less than or equal to variable_2
    (variable_1 != variable_2)  is true if variable_1 does not equal variable_2
There are two ways to make your conditions more reasonable:
If you want two things to be "yes" before running the statement in curly braces, do this:
    if ((variable_1 > 18) && (variable_1 < 21)) 
    {
      ("variable_1 can vote, but can't drink.");
    }
Note that the two "&&" here are the meaning of "and" in JavaScript. Also note that the entire clause has two parts, && must be in parentheses.
If you want one of two things to be true, do this:
    if ((variable_1 == "bananas") || (variable_1 == "JavaScript")) 
    { 
      ("The monkey is happy because it has " +   variable_1);
    }
Back to if exercise!
    <script language="JavaScript">
var color = prompt("What color do you like, red or blue?","");
    var adjective;
    var fontcolor;
    if (color == "red") {
adjective = "Like.";
     fontcolor="red";
    } else if (color == "blue") {
adjective = "Cool.";
     fontcolor="blue";
    } else {
adjective = "Confused.";
     fontcolor="black";
    }
var sentence = "Do you like" + fontcolor + "? Net Monkey thinks you are very " + adjective + "<p>";
    ((fontcolor));
    </script>
Previous page1234567Next pageRead the full text