Home arrow JavaScript arrow The Power of Javascript: Operators concluded

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

The Power of Javascript: Operators concluded
(Page 1 of 5 )

Logical Operators

Logical Operators are useful in situations where you want more than one comparison expression in a statement. Instead of using one expression like this statement: result = x < y; with logical operators the expression x < y can be evaluated and the produced boolean value is assigned to the variable result (because comparison operators produce boolean values). As you can see, using only comparison operators can't produce a complex condition, but using a logical operator with comparison operators can form a statement such that, say, if x is less than y, x equals a certain value, and y is greater than a certain value, produce true; otherwise produce false. As we said, logical and comparison operators are used extensively to help control the script flow of execution and with loops (discussed in the next few articles in details)  So let's take a look at the three available logical operators.


Operator Explanation

Logical AND (&&)

The logical AND operator && (don't confuse it with the Bitwise AND Operator & that works on the binary representation of its integer operands) works with boolean expressions (boolean expressions are those expressions that produces a boolean value; most of the time, you will perform that using the comparison operators) to produce a boolean value. When both of the boolean operands evaluate to true, the logical AND operator returns the value true. It's much more like saying if the right operand is true AND if the left operand is true, then return true. Note that if one of them evaluates to false, the logical AND operator returns false.

This operator is called a short circuit operator, which means that if the Interpreter produces the boolean value false from the left operand, it doesn't evaluate the right operand. Remember, AND produces true only if both of its operands evaluate to true. Why would we need to evaluate the second (right) operand if the first (the left) operand evaluates to false (because AND will produce false even if the second operand evaluates to true as long as the first evaluated to false)?  If the left operand evaluates to true, the interpreter evaluates the right operand.

This is a somewhat advanced topic for you now, so we will discuss it more fully in another article. Note that we have said that true is equal to 1 and false is equal to 0, so we could write both the operands to evaluate to 1 or 0, and the interpreter will understand them as true or false with the logical AND operator, so the statement 
var x = 1 && 0; // produces the value 0
var x = 1 && 1; // produces the value 1
var x = 0 && 1; // produces the value 0

Logical OR

The logical OR operator || (don't confuse it with the Bitwise OR Operator | that works on the binary representation of its integer operands) works with boolean expressions to produce a boolean value. When any of the boolean operands evaluates to true, the logical OR operator produces the value true. It's like saying if the first operand evaluates to true OR the second operand evaluates to true, produce the value true.

This operator is also a short circuit operator, but it makes the interpreter behave differently from the way it behaves with the && operator. With the Operator || the interpreter evaluates the first (left) operand; if it evaluates to true, it produces the value true and escapes the evaluation of the second (right) operand. This makes sense because the || operator evaluates to true if any of its operands are true; we don't need to evaluate the second operand if the first has evaluated to true. The short circuit operation saves the interpreter time. (In a future article I will show you the difference between the Full Evaluation and Short Circuit). Since the interpreter understands 1 as true and 0 as false:
var x = 1 || 0; // produces the value 1
var x = 1 || 1; // produces the value 1
var x = 0 || 0; // produces the value 0

Logical NOT
The Logical Unary Operator NOT ! is used to turn the boolean value of its operand. If x is a boolean variable assigned the value true, the expression !x produces false, and if x is assigned the value false then the expression !x produces the value true.

blog comments powered by Disqus

- 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 

Developer Shed Affiliates


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