The Power of Javascript: Operators concluded - The Void Operator
(Page 4 of 5 )
The void operator is used to set to execute the expression without returning any values. Let's take a look:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Hello World</title>
<script language="JavaScript" type="text/javascript">
var aNumber = void 1;
document.write("aNumber data type = " + aNumber + "<br>");
</script>
</head>
<body>
</body>
</html>
Save and load this code and you will notice that the variable aNumber is assigned the value undefined.

The Ternary Operator :?
This operator is called the ternary conditional operator. To understand why they call it conditional you need to take a look at its three operands. The first operand is a boolean expression. When it evaluates to true the statement in the second operand gets executed, and when it evaluates to false the statement in the third operand get executed. It takes the form
boolean expression ? the true statement : the false statement ;
Note that we can't put the semicolon after the second operand because the colon symbol (:) in the operator separates the second and the third operand. If you put in the semicolon you will get an error; you need to put a semicolon at the third operand. Let's take a look at an example:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Hello World</title>
<script language="JavaScript" type="text/javascript">
var x = 8;
x < 10 && x > 5 ? document.write("Yes, x is less than 10") : document.write("No, x is no less than 10");
</script>
</head>
<body>
</body>
</html>
Save and load the above code and you will get this result.

It's very simple, the expression x < 10 && x > 5 is evaluated, and if it's true then the second operand is executed, which writes to the Web page the string "Yes, x is less than 10." If the expression evaluated to false, then the third operand is executed (not the second one).
Next: Operator Precedence and Associativity >>
More JavaScript Articles
More By Michael Youssef