Table 3.6
Logical operators
|
Operator
|
Usage
|
Description
|
|
&&
|
expr1 && expr2
|
(Logical AND) Returns
expr1
if it can be converted to false; otherwise,
returns expr2.
Thus, when used with Boolean values, &&
returns true if both operands are true;
otherwise, returns false.
|
|
||
|
expr1 || expr2
|
(Logical OR) Returns
expr1
if it can be converted to true; otherwise,
returns expr2.
Thus, when used with Boolean values, || returns
true if either operand is true; if both are
false, returns false.
|
|
!
|
!expr
|
(Logical NOT) Returns false if
its single operand can be converted to true;
otherwise, returns true.
|
Examples of expressions that can be
converted to false are those that evaluate to null, 0, the
empty string (""), or undefined.
The following code shows examples of
the && (logical AND) operator.
a1=true && true
// t && t
returns true
a2=true && false //
t && f returns false
a3=false && true //
f && t returns false
a4=false && (3 == 4) // f && f
returns false
a5="Cat" && "Dog" // t
&& t returns Dog
a6=false && "Cat" // f
&& t returns false
a7="Cat" && false // t
&& f returns false
The following code shows examples of
the || (logical OR) operator.
o1=true || true
// t || t returns
true
o2=false || true // f || t
returns true
o3=true || false // t || f
returns true
o4=false || (3 == 4) // f || f returns false
o5="Cat" || "Dog" // t || t
returns Cat
o6=false || "Cat" // f || t
returns Cat
o7="Cat" || false // t || f
returns Cat
The following code shows examples of
the ! (logical NOT) operator.
n1=!true
//
!t returns false
n2=!false
//
!f returns true
n3=!"Cat"
//
!t returns false
Short-Circuit Evaluation
As logical expressions are evaluated
left to right, they are tested for possible "short-circuit"
evaluation using the following rules:
-
-
false
&& anything is short-circuit evaluated to
false.
-
true ||
anything is short-circuit evaluated to true.
The rules of logic guarantee that these
evaluations are always correct. Note that the anything
part of the above expressions is not evaluated, so any side
effects of doing so do not take effect.
String Operators
In addition to the comparison
operators, which can be used on string values, the
concatenation operator (+) concatenates two string values
together, returning another string that is the union of the
two operand strings. For example, "my " + "string"
returns the string "my string".
The shorthand assignment operator +=
can also be used to concatenate strings. For example, if
the variable mystring has
the value "alpha," then the expression mystring += "bet"
evaluates to "alphabet" and assigns this value to
mystring.
Special Operators
JavaScript provides the following
special operators:
conditional operator
The conditional operator is the only
JavaScript operator that takes three operands. The operator
can have one of two values based on a condition. The syntax
is:
condition ? val1 :
val2
If condition is
true, the operator has the value of val1.
Otherwise it has the value of val2. You can
use the conditional operator anywhere you would use a
standard operator.
For example,
status = (age >= 18) ? "adult" :
"minor"
This statement assigns the value
"adult" to the variable status if
age is
eighteen or more. Otherwise, it assigns the value "minor"
to status.
comma
operator
The comma operator (,) simply
evaluates both of its operands and returns the value of the
second operand. This operator is primarily used inside a
for loop, to
allow multiple variables to be updated each time through
the loop.
For example, if a is a
2-dimensional array with 10 elements on a side, the
following code uses the comma operator to increment two
variables at once. The code prints the values of the
diagonal elements in the array:
for (var i=0, j=9; i <= 9; i++,
j--)
document.writeln("a["+i+","+j+"]= " +
a[i*10 +j])
Note that two-dimensional arrays are
not yet supported. This example emulates a two-dimensional
array using a one-dimensional array.
delete
The delete operator deletes an object,
an object's property, or an element at a specified index in
an array. The syntax is:
delete objectName
delete
objectName.property
delete
objectName[index]
delete property // legal only within a with
statement
where objectName is
the name of an object, property is
an existing property, and index is an
integer representing the location of an element in an
array.
The fourth form is legal only within a
with
statement, to delete a property from an object.
You can use the delete
operator to delete variables declared implicitly but not
those declared with the var
statement.
If the delete
operator succeeds, it sets the property or element to
undefined. The
delete operator returns true if the operation is
possible; it returns false if the operation is not
possible.
x=42
var y= 43
myobj=new Number()
myobj.h=4 // create property
h
delete x // returns
true (can delete if declared implicitly)
delete y // returns
false (cannot delete if declared with var)
delete Math.PI // returns false (cannot delete predefined
properties)
delete myobj.h // returns true (can delete user-defined
properties)
delete myobj // returns true (can delete if
declared implicitly)
Deleting array elements
When you delete an array element, the
array length is not affected. For example, if you delete
a[3], a[4] is still a[4] and a[3] is undefined.
When the delete
operator removes an array element, that element is no
longer in the array. In the following example, trees[3] is
removed with delete.
trees=new
Array("redwood","bay","cedar","oak","maple")
delete trees[3]
if (3 in trees) {
// this does not get executed
}
If you want an array element to exist
but have an undefined value, use the undefined
keyword instead of the delete
operator. In the following example, trees[3] is assigned
the value undefined, but the array element still exists:
trees=new
Array("redwood","bay","cedar","oak","maple")
trees[3]=undefined
if (3 in trees) {
// this gets executed
}
in
The in operator
returns true if the specified property is in the specified
object. The syntax is:
propNameOrNumber in
objectName
where propNameOrNumber
is a string or numeric expression representing a property
name or array index, and objectName is
the name of an object.
The following examples show some uses
of the in operator.
// Arrays
trees=new Array("redwood","bay","cedar","oak","maple")
0 in
trees //
returns true
3 in
trees //
returns true
6 in
trees //
returns false
"bay" in trees // returns false
(you must specify the index number,
//
not the value at that index)
"length" in trees // returns true (length is an Array
property)
// Predefined objects
"PI" in
Math //
returns true
myString=new String("coral")
"length" in myString // returns true
// Custom objects
mycar = {make:"Honda",model:"Accord",year:1998}
"make" in mycar // returns true
"model" in mycar // returns true
instanceof
The instanceof
operator returns true if the specified object is of the
specified object type. The syntax is:
objectName instanceof
objectType
where objectName is
the name of the object to compare to objectType,
and objectType is
an object type, such as Date or Array.
Use instanceof
when you need to confirm the type of an object at runtime.
For example, when catching exceptions, you can branch to
different exception-handling code depending on the type of
exception thrown.
For example, the following code uses
instanceof to
determine whether theDay is a
Date object.
Because theDay is a
Date object,
the statements in the if statement
execute.
theDay=new Date(1995, 12, 17)
if (theDay instanceof Date) {
// statements to execute
}
new
You can use the new operator
to create an instance of a user-defined object type or of
one of the predefined object types Array,
Boolean,
Date,
Function,
Image,
Number,
Object,
Option,
RegExp, or
String. On
the server, you can also use it with DbPool,
Lock,
File, or
SendMail. Use
new as
follows:
objectName = new
objectType ( param1 [,param2]
...[,paramN] )
You can also create objects using
object initializers, as described in "Using Object Initializers" on
page 93.
See new in the
Core JavaScript Reference for more information.
this
Use the this keyword
to refer to the current object. In general,
this
refers to the calling object in a method. Use
this