Assignment Operators


An assignment operator assigns a value to its left operand based on the value of its right operand. The most common assignment operator is = , which just assigns the value of an expression to a variable, like this line of code we've already seen:

 var kilograms = pounds / 2.2046 

The other assignment operators are shorthand assignment operators . How do they work? If you want to add one to a value, you can do so as follows :

 todaysDate = todaysDate + 1 

However, using the += shorthand operator, you can do this a little easier, like this:

 todaysDate += 1 

In other words, the statement todaysDate = todaysDate + 1 does the same thing as the statement todaysDate += 1 . You can use either in your code. Table 2.6 lists the JavaScript shorthand assignment operators.

Table 2.6. JavaScript Shorthand Assignment Operators

Shorthand Operator

Is the Same As

x += y

x = x + y

x -= y

x = x - y

x *= y

x = x * y

x /= y

x = x / y

x %= y

x = x % y

x <<= y

x = x << y

x >>= y

x = x >> y

x >>>= y

x = x >>> y

x &= y

x = x & y

x ^= y

x = x ^ y

x = y

x = x y



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