Operators


At this point, we're ready to start taking the next step up from data storage and start really working with that data. To do that, we'll start working with JavaScript operators . We've already discussed operators briefly ; they enable you to work on your data, as in this case, where I'm adding two numbers with the + addition operator:

 result = 5 + 4 

You also can use operators with the values in variables , as in this case, where I'm assigning the variable numberOfDays the value in the variable numberOfWeeks multiplied by seven, using the * multiplication operator:

 numberOfDays = 7 * numberOfWeeks 

Here's another example; this time, I'm using the JavaScript division operator, / , to convert pounds to kilograms:

(Listing 02-06.html on the web site)
 <HTML>      <HEAD>          <TITLE>              Working With Operators          </TITLE>      </HEAD>      <BODY>          <H1>Working With Operators</H1>          <SCRIPT LANGUAGE="JavaScript">          <!--  var pounds = 5.0   var kilograms = pounds / 2.2046   document.write(pounds + " pounds = " + kilograms + " kilograms.")  // -->          </SCRIPT>       </BODY>  </HTML> 

You can see the results, to an excessive degree of accuracy, in Figure 2.4.

Figure 2.4. Using JavaScript operators.

graphics/02fig04.gif

JavaScript has all kinds of operatorsassignment, comparison, arithmetic, bitwise, logical, string, and special operators. You can find them all listed in Table 2.4, along with what they do.

Table 2.4. The JavaScript Operators

Operator Category

Operator

Description

A RITHMETIC O PERATORS

 

+

(Addition) Adds two numbers together.

 

++

(Increment) Adds one to a variable representing a number.

 

-

(Unary negation, subtraction) As a unary operator, changes the sign of the value of its argument. As a binary operator, subtracts 2 numbers.

 

--

(Decrement) Subtracts one from a variable representing a number.

 

*

(Multiplication) Multiplies two numbers together.

 

/

(Division) Divides two numbers.

 

%

( Modulus ) Returns the integer remainder left after dividing two numbers.

S TRING O PERATORS

 

+

(String addition) Joins two strings together.

 

+=

Concatenates two strings and assigns the result to the first operand.

L OGICAL O PERATORS

 

&&

(Logical AND) When used with Boolean values, && returns true if both operands are true; otherwise , returns false.

 

(Logical OR) When used with Boolean values, returns true if either operand is true; if both are false, it returns false.

 

!

(Logical NOT) Returns false if its single operand can be converted to true; otherwise, returns true.

B ITWISE O PERATORS

&

(Bitwise AND) Returns a one in each bit position if bits of both operands are ones.

 

^

(Bitwise XOR) Exclusive OR. Returns a one in a bit position if bits of one, but not both operands are one.

 

(Bitwise OR) Returns a one in a bit if bits of either operand are one.

 

~

(Bitwise NOT) Flips the bits of its operand.

 

<<

(Left shift) Shifts its first operand the number of bits to the left given in the second operand, shifting in zeros from the right.

 

>>

(Sign-propagating right shift) Shifts the first operand the number of bits to the right given in the second operand, discarding bits shifted off.

 

>>>

(Zero-fill right shift) Shifts the first operand the number of bits to the right given in the second operand, discarding bits shifted off, and shifting in zeros from the left.

A SSIGNMENT O PERATORS

 

=

Assigns the value of the second operand to the first operand.

 

+=

Adds two numbers and assigns the result to the first.

 

-=

Subtracts two numbers and assigns the result to the first.

 

*=

Multiplies two numbers and assigns the result to the first.

 

/=

Divides two numbers and assigns the result to the first.

 

%=

Finds the modulus of two numbers and assigns the result to the first.

 

&=

Performs a bitwise AND and assigns the result to the first operand.

 

^=

Performs a bitwise XOR (exclusive OR) and assigns the result to the first operand.

 

=

Performs a bitwise OR and assigns the result to the first operand.

 

<<=

Performs a left shift and assigns the result to the first operand.

 

>>=

Performs a sign-propagating right shift and assigns the result to the first operand.

 

>>>=

Performs a zero-fill right shift and assigns the result to the first operand.

C OMPARISON O PERATORS

 

==

Equality operator. Returns true if the operands are equal.

 

!=

Returns true if the operands are not equal.

 

===

Strict equality operator. Returns true if the operands are equal and of the same type.

 

!==

Returns true if the operands are not equal and/or not of the same type.

 

>

Greater-than operator. Returns true if the left operand is greater than the right operand.

 

>=

Greater-than-or-equal-to operator. Returns true if the left operand is greater than or equal to the right operand.

 

<

Less-than operator. Returns true if the left operand is less than the right operand.

 

<=

Less-than-or-equal-to operator. Returns true if the left operand is less than or equal to the right operand.

S PECIAL O PERATORS

 

?:

The conditional operator. Performs a simple "if else" test.

 

,

The comma operator. Evaluates two expressions. Returns the result of the second expression.

 

delete

Deletes an object, an object's property, or an element at a given index in an array.

 

function

Defines an anonymous function (discussed in Chapter 3).

 

in

Returns true if the given property is in the given object.

 

instanceof

Returns true if the given object is of the given object type.

 

new

Creates an new object from a user -defined object type or a built-in object type.

 

typeof

Returns a string indicating the type of its operand.

 

void

Lets an expression be evaluated without returning a value.

Which operators are supported in which browsers? You'll find that information in Table 2.5.

Table 2.5. JavaScript Operators by Browser

Operator

NS2

NS3

NS4

NS6

IE3a

IE3b

IE4

IE5

IE5.5

IE6

Arithmetic

x

x

x

x

x

x

x

x

x

x

Assignment

x

x

x

x

x

x

x

x

x

x

Comparison

x

x

x

x

x

x

x

x

x

x

Bitwise

x

x

x

x

x

x

x

x

x

x

Logical

x

x

x

x

x

x

x

x

x

x

String

x

x

x

x

x

x

x

x

x

x

Conditional

x

x

x

x

x

x

x

x

x

x

Comma

x

x

x

x

x

x

x

x

x

x

delete

 

x

x

x

   

x

x

x

x

function

     

x

       

x

x

in

     

x

       

x

x

instanceof

     

x

     

x

x

x

new

x

x

x

x

x

x

x

x

x

x

typeof

 

x

x

x

x

x

x

x

x

x

void

 

x

x

x

x

x

x

x

x

x

Now that we're talking about operators, it's time to introduce a couple of terms, such as an expression . An expression is anything JavaScript can evaluate to get a single value. For example, 5 is a simple expression that evaluates, simply, to 5. The expression 5 + 4 evaluates to 9. If myValue holds 10, the expression myValue * 2 evaluates to 20. You'll see the term "expression" often in JavaScript documentation, and now you know what it meansa term or group of terms that JavaScript can evaluate to a single value.

In addition, in JavaScript, operators operate on operands . In the expression 5 + 4 , for example, the operator is the + addition operator, 5 is the left operand, and 4 is the right operand. An operator that takes just one operand is a unary operator, one that takes two is a binary operator, and one that takes three is a tertiary operator. And that's it; now we're ready to start taking a look at the operators JavaScript offers us in detail, starting with the assignment operators.



Inside Javascript
Inside JavaScript
ISBN: 0735712859
EAN: 2147483647
Year: 2005
Pages: 492
Authors: Steve Holzner

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net