Conditionals : If : Statement JAVASCRIPT TUTORIALS


JAVASCRIPT TUTORIALS » Statement » If »

 

Conditionals














The format of a simple if statement looks like the following:








if (expression)
      statement;



If the 'expression' in parentheses evaluates to true, the statement is executed; otherwise, the statement is skipped.

If two or more lines of code are to be executed, curly braces {} must be used to designate what code belongs in the if statement.

The keyword else extends the functionality of the basic if statement by providing other alternatives.

The format of an if...else combination looks like the following:








if (expression)
      statement1;
   else
      statement2;



if the expression evaluates to true, statement1 is executed, otherwise, statement2 is executed.












<html>
<SCRIPT LANGUAGE='JavaScript'>
    <!--
    var varA = 2;
    var varB = 5;

    if (varA == 0)
      document.write("varA == 0");
    else {
      if ((varA * 2>= varB)
        document.write("(varA * 2) >= varB");
      else
        document.write("(varA * 2) < varB");
      document.write(varB);
    }
    //-->
</SCRIPT>
</html>







HTML code for linking to this page:

Follow Navioo On Twitter

JAVASCRIPT TUTORIALS

 Navioo Statement
» If