please dont rip this site

JavaScript: The Definitive Guide

Previous Chapter 4
Expressions and Operators
Next
 

4.8 Assignment Operators

As we saw in the discussion of variables in Chapter 3, Variables and Data Types, = is used in JavaScript to assign a value to a variable. For example:

i =index.html 0
While you might not normally think of such a line of JavaScript as an expression that has a value and can be evaluated, it is in fact an expression, and technically speaking, = is an operator.

The = operator expects its left-hand operand to be a variable, or the element of an array or a property of an object, and expects its right-hand operand to be an arbitrary value of any type. The value of an assignment expression is the value of the right-hand operand. As a side effect, the = operator assigns the value on the right to the variable, element, or property on the left so that future uses of the variable, element, or property refer to the value.

Because = is defined as an operator, you can include it in more complex expressions. For example, you can assign and test a value in the same expression with code like this:

(a =index.html b) == 0
If you do this, be sure you are clear on the difference between the = and == operators!

The assignment operator has right-to-left associativity, which means that when multiple assignment operators appear in an expression, they are evaluated from right to left. This means that you can write code like the following to assign a single value to multiple variables:

i =index.html j = k = 0;

Remember that each assignment expression has a value that is the value of the right-hand side. So in the above code, the value of the first assignment (the rightmost one) becomes the right-hand side for the second assignment (the middle one) and this value becomes the right-hand side for the last (leftmost) assignment.

As we'll see in Chapter 7, Objects, you can use the Object.assign() method in Navigator 3.0 to override the behavior of the assignment operator.

Assignment with Operation

Besides the normal = assignment operator, JavaScript also supports a number of other assignment operators that provide a shortcut by combining assignment with some other operation. For example, the += operator performs addition and assignment. The following expression:

total +=index.html sales_tax
is equivalent to this one:

total =index.html total + sales_tax

As you might expect, the += operator works for numbers or strings. For numeric operands, it performs addition and assignment, and for string operands, it performs concatenation and assignment.

Similar operators include -=, *=, &=, and so on. Table 4.2 lists them all. In general, the expression:

a op=index.html b
where op is an operator, is equivalent to:

a =index.html a op b

Table 4.2: Assignment Operators
Operator Example Equivalent
+= a +=index.html b a =index.html a + b
-= a -=index.html b a =index.html a - b
*= a *=index.html b a =index.html a * b
/= a /=index.html b a =index.html a / b
%= a %=index.html b a =index.html a % b
<<= a <<=index.html b a =index.html a << b
>>= a >>=index.html b a =index.html a >> b
>>>= a >>>=index.html b a =index.html a >>> b
&= a &=index.html b a =index.html a & b
|= a |=index.html b a =index.html a | b
^= a ^=index.html b a =index.html a ^ b


Previous Home Next
Bitwise Operators Book Index Miscellaneous Operators

HTML: The Definitive Guide CGI Programming JavaScript: The Definitive Guide Programming Perl WebMaster in a Nutshell

file: /Techref/language/java/SCRIPT/definitive/ch04_08.htm, 9KB, , updated: 2019/10/14 15:00, local time: 2024/11/23 10:04,
TOP NEW HELP FIND: 
18.117.75.218:LOG IN

 ©2024 These pages are served without commercial sponsorship. (No popup ads, etc...).Bandwidth abuse increases hosting cost forcing sponsorship or shutdown. This server aggressively defends against automated copying for any reason including offline viewing, duplication, etc... Please respect this requirement and DO NOT RIP THIS SITE. Questions?
Please DO link to this page! Digg it! / MAKE!

<A HREF="http://techref.massmind.org/Techref/language/java/SCRIPT/definitive/ch04_08.htm"> [Chapter 4] 4.8 Assignment Operators</A>

After you find an appropriate page, you are invited to your to this massmind site! (posts will be visible only to you before review) Just type a nice message (short messages are blocked as spam) in the box and press the Post button. (HTML welcomed, but not the <A tag: Instead, use the link box to link to another page. A tutorial is available Members can login to post directly, become page editors, and be credited for their posts.


Link? Put it here: 
if you want a response, please enter your email address: 
Attn spammers: All posts are reviewed before being made visible to anyone other than the poster.
Did you find what you needed?

 

Welcome to massmind.org!

 

Welcome to techref.massmind.org!

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

  .