Online Documentation Server
 ПОИСК
ods.com.ua Web
 КАТЕГОРИИ
Home
Programming
Net technology
Unixes
Security
RFC, HOWTO
Web technology
Data bases
Other docs

 


 ПОДПИСКА

 О КОПИРАЙТАХ
Вся предоставленная на этом сервере информация собрана нами из разных источников. Если Вам кажется, что публикация каких-то документов нарушает чьи-либо авторские права, сообщите нам об этом.




Previous Table of Contents Next

–17=>111111111111111111111111111011112
–17>>>0=> 11111111111111111111111111101111
–17>>>3=>   11111111111111111111111111101111
=> 00011111111111111111111111111101
=>111111111111111111111111111012=>536870909

Don’t worry if the bitwise shift operators seem difficult. Chances are you’ll never need to use them.

Assignment Operators

operand1 &127; = operand2

Assignment operators are binary operators handling arithmetic, string, or bitwise operators. They perform the regular operation (&127;) on the operands and assign the result to the first operand. The assignment operators are as follows:

Table 7-8. Assignment operators.


Syntax Name

= Equals
+= Add/concatenate by value
–= Subtract by value
*= Multiply by value
/= Divide by value
%= Modulo by value
&= Bitwise AND by value
|= Bitwise OR by value
^= Bitwise XOR
<<= Left shift by value
>>= Right shift by value
>>>= Zero-fill right shift by value

You already know what the simple = assignment operator does—it stores the value of the expression in the data structure. For all the other operators, JavaScript pretends that

var1 &127; = var2

is

var1 = var1 &127; var2

For example:

counter =>> 2

shifts the value of counter two positions to the right, and

text += " Gates"

attaches the word Gates to the end of the string stored in text. The same rule applies to all assignment operators listed in Table 7-8.

Since the assignment statements are evaluated from right to left, you can use multiple operators in the same statement. The rule is that the value to the right of the operator is evaluated and then assigned to the variable to the left of the operator.

num1 = num2 = num3 = num4 = num5

The value of num5 is assigned to num4, the value of num4 is assigned to num3, and so on. After such an operation, all five variables hold the same value.

Relational Operators

operand1 _ operand2

Relational operators, also called comparison operators, compare two values and return a Boolean result. All relational operators are binary, because they compare two values. These operators are often used in conditional statements, covered in Chapter 8, Control Structures. Here is the complete list of JavaScript’s relational operators:

Table 7-9. Relational operators


Syntax Name

== equals
!= not equal
< less than
<= less than or equal to
> greater than
>= greater than or equal to

These operators take numeric or string values as their operands. Numeric comparison is usually very simple:

2 == 1 // evaluates to false
99.0 == 99 // evaluates to true
2 != 1 // evaluates to true
3 < 2.5 // evaluates to false
2 <= 2 // evaluates to true
9 > 9 // evaluates to false
9 >= –10 // evaluates to false

String operands are compared according to the ASCII value of their characters. This comparison is similar to the one by which words are placed in a dictionary. The only difference is that instead of using a single set of letters, JavaScript uses 256 different characters. As you know, each character has a numeric value. This value determines if a character is greater than, equal to, or less than another one.

When you compare multicharacter strings, the first character of the first operand is compared with the first character of the second operand. The second characters are compared only if the comparison of the first ones indicates equality. The process continues until the corresponding characters are not equal, or until the end of one of the strings is reached. Here are some characters and their ASCII values:

0 – 48 A – 65 a – 97
9 – 57 Z – 90 z – 122

You can find the full ASCII table at the end of the book.

Here is an example:

"computerA" > "computerB"
"c" == "c"
"o" == "o"
"m" == "m"
"p" == "p"
"u" == "u"
"t" == "t"
"e" == "e"
"r" == "r"
"A" < "B"  => ("computerA" > "computerB"=> false)

Take a look at the following results for a better understanding:

"JavaScript" == "javascript" // evaluates to false
"bill" != "bill" // evaluates to false
" " < "  " // ([one space] < [two spaces]) evaluates to true
16 <= "16" // evaluates to true
"luck" > "Work" // evaluates to true!
"XT" >= "pentium pro" // evaluates to false

Equality and Inequality

As you can see, the Boolean equality operator (==) is similar to the assignment operator (=). This similarity is the source of many programming errors, not only in JavaScript but also in other languages such as C, C++, and Java. Equality operators are often used in if-else statements, where the assignment operator cannot be used. (JavaScript does not allow side effects in a conditional statement.) In this case the interpreter produces a meaningful error saying: “test for equality (==) mistyped as assignment (=)? Assuming equality test.” However, in some other situations, both the equality and assignment operators are valid and the browser, rightly so, does not generate any errors. This is the reason why such errors are very difficult to debug. The following example demonstrates a situation in which both operators are acceptable:

var i = 1
document.write(i = 2)

var j = 1
document.write(j == 2)

The first printing statement prints 2, because an assignment expression evaluates to the assigned value. The second printing statement prints false under Netscape Navigator, and 0 on MSIE (MSIE converts the Boolean value false to 0 for printing, rather than to "false"), because 1 is not equal to 2. In a long script, if you accidentally replaced one operator by the other, you would have a hard time finding the mistake.

Previous Table of Contents Next


With any suggestions or questions please feel free to contact us