Understanding Operators


Operators are the glue that combines identifiers, variables, and keywords together to create statements. In this chapter, you’ve already used the concatenation operator—and probably are pretty familiar with some other operators, such as those used in everyday arithmetic.

The following sections explain the operators you’re most likely to encounter. (You’ll find information about the operators specifically relating to JavaScript objects, such as typeof and new, in Chapter 7, “ Working with Objects.”)

Using Assignment Operators

One JavaScript operator, the simple assignment operator, represented by an equals sign, has already been frequently used to create statements in this chapter. For example, this statement:

 var test = "The answer is " + 42; 

uses the assignment operator to store the value "The answer is 42" in the variable test. Put more generally, the assignment operator assigns the value on the right of the equals sign to the variable on the left.

JavaScript has other assignment operators you can use, all of which assign a value to a left operand based on the value of the right operand. In other words, these operators combine two operations into one step. Here are the “combination” operators:

  • x += y assigns the value x + y to the variable x.

    This works for string concatenation as well as numerical addition.

  • x = y assigns the value x y to the variable x.

  • x *= y assigns the value x * y to the variable x.

  • x /= y assigns the value x / y to the variable x.

  • x %= y assigns the value x modulo y

    (the remainder following division of x by y) to the variable x.

Advacned

For example, the modulo assignment operator (%=) combines assignment with modulo (or remainder) arithmetic. If you run this:

 var myAssign = 13;  myAssign %= 3; // modulo assignment  document.write("13 modulo 3 is " + myAssign); 

the modulus—or integer remainder when you divide 13 by 3—will be displayed, as shown in Figure 2-7.

click to expand
Figure 2-7: The modulo operator combines remainder arithmetic with assignment.

Using Arithmetic Operators

In addition to the standard arithmetic operators—plus (+), minus (–), multiplication (*), division (/)—JavaScript supports the arithmetic operators shown in Table 2-4.

Table 2-4: JavaScript Arithmetic Operators (in Addition to Standard Arithmetic Operators)

Operator

Functionality

% (Modulus operator)

x % y returns x modulo y (the integer remainder of x divided by y).

++ (Increment operator)

Adds one to the operand. If the increment operator is used as a prefix (++x), the operator returns the value after incrementing. If it is used as a postfix (x++), the value is returned before incrementing.

-- (Decrement operator)

Works the same way as the increment operator, except subtracts one rather than adds one.

Advacned

Listing 2-5 illustrates how the increment operator works (the decrement operator works the same way except that it subtracts rather than adds).

Listing 2.5: Working with the Increment Operator

start example
 var Inc = 42;  document.write("Starting value of " + Inc + "<BR>");  var IncPreFix = ++Inc;  var IncPostFix = Inc++;  document.write(" IncPreFix " + IncPreFix + "<BR>");  document.write(" IncPostFix " + IncPostFix+ "<BR>");  document.write("Ending value of " + Inc); 
end example

In Listing 2-5, the variable Inc starts with a value of 42. This statement:

 var IncPreFix = ++Inc; 

increases the value of Inc by one, so it’s now 43, and then assigns the value 43 to the variable IncPreFix.

Next, this statement:

 var IncPostFix = Inc++; 

first assigns the current value of Inc (43) to IncPostFix and only then increments Inc so that it finally stores 44. Figure 2-8 shows the results of these increment operations.

click to expand
Figure 2-8: Results of the increment operations

Using Boolean Operators

Advacned

Boolean, or logical, operators let you combine the results of logical evaluation, as shown in Table 2-5, to create logical expressions. In JavaScript, a logical expression consists of values of the Boolean type and Boolean operators. Every logical expression evaluates to true or false.

Table 2-5: Boolean Operators

Operator

Meaning

& & (And)

A & & B is true if A is true and B is true.

|| (Or)

A || B is true if A is true or B is true.

! (Not)

!A is true if A is false, and false if A is true.

JavaScript Boolean expressions are evaluated from left to right. They’re processed using “incomplete” evaluation as follows:

  • False & & anything always evaluates to false without looking at the anything.

  • True || anything always evaluates to true without looking at the anything.

Programmers write code that uses incomplete evaluation as a shortcut to save computer processing time. Of course, it always leads to the correct Boolean result. However, any code to the right of the Boolean operator will not be evaluated (or triggered). In some situations, this might cause unexpected errors or side effects, so you shouldn’t write code that depends on the execution of expressions to the right of a Boolean operator unless you are very careful!

Using Comparison Operators

A comparison operator compares its operands and returns a logical value of true or false depending on the results of the comparison. Comparison of strings is case sensitive and is based on a dictionary ordering of the strings. So, this string:

 "Harry Potter" == "harry potter"; 

returns a value of false because upper-case letters aren’t the same as lower-case letters.

However, the following:

 "Dumbledore" < "Voldemort"; 

evaluates to true because D comes before V in the dictionary.

Table 2-6 shows JavaScript comparison operators.

Table 2-6: JavaScript Comparison Operators

Operator

Meaning

==

Equality. This returns true if its two operands are equal.

!=

Inequality, the exact opposite of equality; if its two operands are equal, it returns false.

===

Identity. Evaluatesto true if its two operands are equal without any type conversion.

!==

Nonidentity, the exact opposite of identity; it evaluates totrue if its operands have different types or values.

>

Greater than.

>=

Greater than or equal.

<

Less than.

<=

Less than or equal.

Using Operator Precedence

Operator precedence means the order in which operators are applied when evaluating an expression. Order does matter: The order in which operators are applied can change the value of an expression.

You can direct JavaScript to evaluate part of an expression first by enclosing it in parentheses. In the absence of this kind of direction, evaluation is probably more or less what you might expect. The following operators appear in the order of JavaScript evaluation precedence (the highest precedence is first):

  • Negation, increment, decrement

  • Multiply, divide, modulo

  • Addition, subtraction

  • Comparison

  • Logical And

  • Logical Or

  • Assignment




Learn How to Program Using Any Web Browser
Learn How to Program Using Any Web Browser
ISBN: 1590591135
EAN: 2147483647
Year: 2006
Pages: 115
Authors: Harold Davis

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