Home arrow JavaScript arrow Page 4 - The Power of Javascript: Operators concluded
JAVASCRIPT

The Power of Javascript: Operators concluded


This is the last article in "The Power of Javascript" series covering operators. In this part, we discuss the logical operators, the operator typof, the void operator, the ternary operator :?, and operators' precedence and Associativity. You may not realize the power and usefulness of operators yet, but when we discuss how you can control your script flow of execution with if/else statements and loop statements, you will realize what operators can do for you, especially the logical operators and the comparison operators.

Author Info:
By: Michael Youssef
Rating: 5 stars5 stars5 stars5 stars5 stars / 6
August 02, 2005
TABLE OF CONTENTS:
  1. · The Power of Javascript: Operators concluded
  2. · Logical Operators Example
  3. · The Operator typeof
  4. · The Void Operator
  5. · Operator Precedence and Associativity

print this article
SEARCH DEVARTICLES

TOOLS YOU CAN USE

advertisement
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).


blog comments powered by Disqus
JAVASCRIPT ARTICLES

- More Top jQuery Tutorials for Beginners
- More Top jQuery Plugins for Menus
- Top jQuery Tutorials for Beginners
- New UI Framework and SDK for JavaScript Rele...
- JavaScript OpenPGP Tool, Node.js 0.6.3 Avail...
- Yahoo Releases Cocktails Language and Develo...
- Customizing jQuery Slideshows: Dynamic Contr...
- Customizing jQuery Slideshows: the animate()...
- Customizing jQuery Slideshows: slideUp() and...
- Customizing jQuery Slideshows: hide() and sh...
- Web Workers: Performing Calculations in Para...
- More Top JavaScript Frameworks and Libraries
- More Dynamic jQuery Styling Techniques
- The Top JavaScript Libraries
- The Top JavaScript Frameworks

Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 



© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 9 - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials