JAVASCRIPT


  CONDITIONAL INSTRUCTION „IF”

 if (condition) instruction;
 if (condition) instruction; else instruction;
 if (condition) {instructions} else {instructions};


  SHORT „IF” INSTRUCTIONS

  (condition) ? {instructions} : {instructions}

This instruction replaces the "if" command that would be in the form:
if (condition) {instructions} else {instructions}



Example (complete HTML and JS code):
<html >
<head >
<script type="text/javascript">
var c = null;
function time()
{
var dat =new Date();
var day =dat.getDate();
var month =dat.getMonth()+1;
var year =dat.getFullYear();
var hour =dat.getHours();
var min =dat.getMinutes();
var sec =dat.getSeconds();

hour=hour + ((min<10)?":0":":") + min;
// shortened "if" - addition of a zero with a colon or a colon itself
hour=hour + ((sec<10)?":0":":") + sec;
// shortened "if" - addition of a zero with a colon or a colon itself
if (month<10) {month="0"+month;}
// addition of zero if the month number is a one-digit number
if (day<10) {day="0"+day;}
// addition of zero if the day number is a one-digit number

document.getElementById ("show"). value=
" DATE:" + day + "." + month + "." + year + " | TIME: " + hour;
c = setTimeout("time()", 1000);
}
</script >
</head >
<body onLoad="time()">
<input type="text" id="show" size="40"
style="color:blue; font size:20px; ">
</body >
</html >


The result of the script is displayed in the text field "input", whose ID is "show": In the function "time()" the handle for this element "input" has been used and its value has been given.

  document. getElementById("show"). value = ... ;

In the abbreviated "if" version, note the different meanings of colons.

  hour = hour + ((min<10)? ":0" : ":") + min;


  INSTRUCTION „SWITCH”

The "switch" instruction is used to replace many "if" instructions when it is necessary to choose the answer to the question what is the value of the given expression.

switch(expression)
{
case value1: instructions1; break;
case value2: instructions2; break;
case value3: instructions3; break;
case value4: instructions4; break;
case value5: instructions5; break;
case value6: instructions6; break;
default: instructions7;
}


Example:

switch (fate)
{
case 11: document.write(” You have won a car.”); break;
case 27: document.write(” You have won a book.”); break;
case 35: document.write(” You have won chewing gum.”); break;
default: document.write(” You didn't win anything!”); break;
}