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

The increment operator is a unary operator, which can be used in either suffix or prefix notations. It increments the operand’s value by 1. If used after the operand (suffix), the operator returns the value of the operand before incrementing it. If used before the operand (prefix), the operator returns the value of the operand after incrementing it. Understanding these differences is important when you use such operations as side effects of other statements, such as assignment ones. The following set of statements outlines this concept:

var a = 1
var b = ++a // prefix
document.write("a is ", a, ", b is ", b) // a is 2, b is 2

The first statement assigns the value 1 to a. The second statement performs two different actions:

  • Increments a to 2
  • Assigns a’s new value to b

The increment operator in suffix notation performs the actions in reverse order, and therefore the results differ. Suffix notation is demonstrated in the following code:

var a = 1
var b = a++ // suffix
document.write("a is ", a, ", b is ", b) // a is 2, b is 1

b is assigned the value of a, and then a is incremented.

Dual actions in one statement are discussed at the end of this chapter. Generally, you should avoid using such side effects. The previous code would be simpler had it looked like:

var a = 1
var b = a
a++
document.write("a is ", a, ", b is ", b) // a is 2, b is 1

The increment operator takes only data structures as its operand. That is, you can only use it on a variable or a property of an existing object, but not on a literal.

It is natural to come to a conclusion that incrementing is the same as adding 1 to the value:

var a = 1
var b = 1
a++
b = b + 1 // equivalent to b += 1 (see assignment operators)

This is true as far as correctness of the script is concerned. It is incorrect if performance is important. The advantage of incrementing is that it is much faster than standard assignment (fourth line in above code section). You should always increment when you want to add 1 to a variable (or to a property of an object). It is not so important when the addition operation is done a few times. You will definitely feel the difference when you have 100,000 addition operations. Another benefit of the incrementing operator is that it is much easier to understand a statement like countTemp++ than countTemp = countTemp + 1.

It is important to remember that Boolean expressions are equivalent to 1 and 0 in certain situations. The following statements show the effect of incrementing Boolean variables:

var a = true
var b = false
a++
b++
document.write("a is ", a, ", b is ", b) // a is 2, b is 1


Caution:  Microsoft Internet Explorer 3.0x generates an error when you use prefix notation as an independent statement. The following script segment does not work under MSIE, so use suffix notation to increment a value:

var num = 5
++num // error under MSIE

Note that this rule does not apply to increment operators in other positions, such as for loops. Furthermore, you are free to use both notations as side effects, because the bug only applies to stand-alone statements that increment a value.


Decrement

operand1––
––operand1

The decrement operator is similar to the increment operator. It decreases the value of the operand by one, whereas the increment operator increases it by one.

Negation

–operand1

The negation operator precedes a numeric value (a variable, a property of an existing object, or a numeric literal). By placing this operator before its operand (do not insert any space characters), JavaScript evaluates a positive number as its corresponding negative number, and vice versa. As before, you might think that this operator can be replaced by a statement in which the operand is multiplied by –1. Once again, this is a mistake. Due to the internal structure of the JavaScript interpreter, and the negation operator specifically, negating a numeric value using the negation operator is faster than multiplying it by –1. If you are a traditional Pascal programmer, it might take you a while to get used to the negation and incremental operators, but it is worth the effort!

var a = 3
var b = 9
–a + b // evaluates to 6
–b // evaluates to –9

String Operators

operand1 + operand2

The string operator’s syntax is identical to that of the addition operator. They differ in the type of operands they operate on. This operator accepts any values as operands, provided that at least one of them is a string. A string is actually an object, so it can be said that the string operator operates on string objects. It joins them together, as in

"Ladies" + "and " + "gentlemen"

The string operator can operate on more than two operands, but it is still a binary operator, because of the way it works. It concatenates the first two strings, then concatenates the third string to the accumulated string, and so on. If one of the operands is not a string, it is automatically cast to a string. The string operator is also called a concatenation operator.

An expression consisting of numerous string operators evaluates to a single string. Based on that, here are two different statements:

document.write("I have" + 2 + " cookies.")
document.write("I have", 2, "cookies.")

Previous Table of Contents Next


With any suggestions or questions please feel free to contact us