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 / 16
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

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

- Project Nashorn to Make Java, JavaScript Wor...
- JavaScript Virus Attacks Tumblr Blogs
- Google Releases Stable Dart Version, JavaScr...
- Khan Academy Unveils New JavaScript Learning...
- Accessing Nitro? There`s an App for That
- JQuery 2.0 Leaving Older IE Versions Behind
- Fastest JavaScript Engine Might Surprise You
- Microsoft Adjusting Chakra for IE 10
- Brendan Eich: We Don`t Need Google Native Cl...
- An Overview of JavaScript Statements
- An Overview of JavaScript Operators
- Overview of JavaScript Variables
- More of the Top jQuery Social Plugins
- The Top jQuery Social Plugins
- More of the Top jQuery Slider Plugins

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 

Developer Shed Affiliates

 




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